Example #1
0
        /// <summary>
        /// Initiates the parsing process.  Not thread safe and should only be called once on a parsing context
        /// </summary>
        /// <param name="yamlDocument">Yaml document to parse.</param>
        /// <param name="diagnostic">Diagnostic object which will return diagnostic results of the operation.</param>
        /// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
        internal OpenApiDocument Parse(YamlDocument yamlDocument, OpenApiDiagnostic diagnostic)
        {
            RootNode = new RootNode(this, diagnostic, yamlDocument);

            var inputVersion = GetVersion(RootNode);

            OpenApiDocument doc;

            switch (inputVersion)
            {
            case string version when version == "2.0":
                VersionService = new OpenApiV2VersionService();
                doc            = VersionService.LoadDocument(RootNode);
                diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
                break;

            case string version when version.StartsWith("3.0"):
                VersionService = new OpenApiV3VersionService();

                doc = VersionService.LoadDocument(RootNode);
                diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
                break;

            default:
                throw new OpenApiUnsupportedSpecVersionException(inputVersion);
            }

            return(doc);
        }
Example #2
0
        /// <summary>
        /// Initiates the parsing process.  Not thread safe and should only be called once on a parsing context
        /// </summary>
        /// <param name="yamlDocument"></param>
        /// <param name="diagnostic"></param>
        /// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
        internal OpenApiDocument Parse(YamlDocument yamlDocument, OpenApiDiagnostic diagnostic)
        {
            RootNode = new RootNode(this, diagnostic, yamlDocument);

            var inputVersion = GetVersion(RootNode);

            OpenApiDocument doc;

            if (inputVersion == "2.0")
            {
                VersionService = new OpenApiV2VersionService();
                doc            = this.VersionService.LoadDocument(this.RootNode);
            }
            else if (inputVersion.StartsWith("3.0."))
            {
                this.VersionService = new OpenApiV3VersionService();
                doc = this.VersionService.LoadDocument(this.RootNode);
            }
            else
            {
                // If version number is not recognizable,
                // our best effort will try to deserialize the document to V3.
                this.VersionService = new OpenApiV3VersionService();
                doc = this.VersionService.LoadDocument(this.RootNode);
            }
            return(doc);
        }
        public void ParseSecuritySchemeReference()
        {
            // Arrange
            var versionService = new OpenApiV2VersionService();
            var referenceType  = ReferenceType.SecurityScheme;
            var id             = "securitySchemeId";
            var input          = $"{id}";

            // Act
            var reference = versionService.ConvertToOpenApiReference(input, referenceType);

            // Assert
            reference.Type.Should().Be(referenceType);
            reference.ExternalResource.Should().BeNull();
            reference.Id.Should().Be(id);
        }
        public void ParseLocalSchemaReference()
        {
            // Arrange
            var versionService = new OpenApiV2VersionService();
            var referenceType  = ReferenceType.Schema;
            var id             = "parameterId";
            var input          = $"#/definitions/{id}";

            // Act
            var reference = versionService.ConvertToOpenApiReference(input, referenceType);

            // Assert
            reference.Type.Should().Be(referenceType);
            reference.ExternalResource.Should().BeNull();
            reference.Id.Should().Be(id);
        }
        public void ParseExternalReference()
        {
            // Arrange
            var versionService   = new OpenApiV2VersionService();
            var externalResource = "externalSchema.json";
            var id    = "externalPathSegment1/externalPathSegment2/externalPathSegment3";
            var input = $"{externalResource}#/{id}";

            // Act
            var reference = versionService.ConvertToOpenApiReference(input, null);

            // Assert
            reference.ExternalResource.Should().Be(externalResource);
            reference.Type.Should().BeNull();
            reference.Id.Should().Be(id);
        }
Example #6
0
        /// <summary>
        /// Initiates the parsing process of a fragment.  Not thread safe and should only be called once on a parsing context
        /// </summary>
        /// <param name="yamlDocument"></param>
        /// <param name="version">OpenAPI version of the fragment</param>
        /// <param name="diagnostic">Diagnostic object which will return diagnostic results of the operation.</param>
        /// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
        internal T ParseFragment <T>(YamlDocument yamlDocument, OpenApiSpecVersion version, OpenApiDiagnostic diagnostic) where T : IOpenApiElement
        {
            var node = ParseNode.Create(this, diagnostic, yamlDocument.RootNode);

            T element = default(T);

            switch (version)
            {
            case OpenApiSpecVersion.OpenApi2_0:
                VersionService = new OpenApiV2VersionService();
                element        = this.VersionService.LoadElement <T>(node);
                break;

            case OpenApiSpecVersion.OpenApi3_0:
                this.VersionService = new OpenApiV3VersionService();
                element             = this.VersionService.LoadElement <T>(node);
                break;
            }

            return(element);
        }