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);
        }
        public void ParseLocalSchemaReference()
        {
            // Arrange
            var versionService = new OpenApiV3VersionService();
            var referenceType  = ReferenceType.Schema;
            var id             = "schemaId";
            var input          = $"#/components/schemas/{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 ParseSecuritySchemeReference()
        {
            // Arrange
            var versionService = new OpenApiV3VersionService();
            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 ParseExternalReference()
        {
            // Arrange
            var versionService   = new OpenApiV3VersionService();
            var externalResource = "externalSchema.json";
            var id    = "externalPathSegment1/externalPathSegment2/externalPathSegment3";
            var input = $"{externalResource}#/{id}";

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

            // Assert
            reference.Type.Should().BeNull();
            reference.ExternalResource.Should().Be(externalResource);
            reference.Id.Should().Be(id);
        }