Ejemplo n.º 1
0
 private static Func <OpenApiDocument, JsonReferenceResolver> CreateReferenceResolverFactory()
 {
     return(document =>
     {
         var schemaResolver = new OpenApiSchemaResolver(document, new JsonSchemaGeneratorSettings());
         return new JsonAndYamlReferenceResolver(schemaResolver);
     });
 }
Ejemplo n.º 2
0
        /// <summary>Creates a Swagger specification from a JSON string.</summary>
        /// <param name="data">The JSON data.</param>
        /// <param name="documentPath">The document path (URL or file path) for resolving relative document references.</param>
        /// <param name="expectedSchemaType">The expected schema type which is used when the type cannot be determined.</param>
        /// <param name="referenceResolverFactory">The JSON reference resolver factory.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The <see cref="OpenApiDocument"/>.</returns>
        public static async Task <OpenApiDocument> FromJsonAsync(string data, string documentPath, SchemaType expectedSchemaType,
                                                                 Func <OpenApiDocument, JsonReferenceResolver> referenceResolverFactory, CancellationToken cancellationToken = default)
        {
            // For explanation of the regex use https://regexr.com/ and the below unescaped pattern that is without named groups
            // (?:\"(openapi|swagger)\")(?:\s*:\s*)(?:\"([^"]*)\")
            var pattern = "(?:\\\"(?<schemaType>openapi|swagger)\\\")(?:\\s*:\\s*)(?:\\\"(?<schemaVersion>[^\"]*)\\\")";
            var match   = Regex.Match(data, pattern, RegexOptions.IgnoreCase);

            if (match.Success)
            {
                var schemaType    = match.Groups["schemaType"].Value.ToLower();
                var schemaVersion = match.Groups["schemaVersion"].Value.ToLower();

                if (schemaType == "swagger" && schemaVersion.StartsWith("2"))
                {
                    expectedSchemaType = SchemaType.Swagger2;
                }
                else if (schemaType == "openapi" && schemaVersion.StartsWith("3"))
                {
                    expectedSchemaType = SchemaType.OpenApi3;
                }
            }

            if (expectedSchemaType == SchemaType.JsonSchema)
            {
                throw new NotSupportedException("The schema type JsonSchema is not supported.");
            }

            var contractResolver = GetJsonSerializerContractResolver(expectedSchemaType);

            return(await JsonSchemaSerialization.FromJsonAsync <OpenApiDocument>(data, expectedSchemaType, documentPath, document =>
            {
                document.SchemaType = expectedSchemaType;
                if (referenceResolverFactory != null)
                {
                    return referenceResolverFactory(document);
                }
                else
                {
                    var schemaResolver = new OpenApiSchemaResolver(document, new JsonSchemaGeneratorSettings());
                    return new JsonReferenceResolver(schemaResolver);
                }
            }, contractResolver, cancellationToken).ConfigureAwait(false));
        }