Ejemplo n.º 1
0
        public async Task WriteJsonSchemaAsync <T>(IFile file)
        {
            await using (var stream = file.OpenWrite())
            {
                await using (var textWriter = new StreamWriter(stream))
                {
                    var jsonSchema     = GetSchema <T>();
                    var jsonSchemaType = jsonSchemaGeneratorSettings.SchemaType;

                    var json = JsonSchemaSerialization.ToJson(
                        jsonSchema,
                        jsonSchemaType,
                        JsonSchema.CreateJsonSerializerContractResolver(jsonSchemaType),
                        Formatting.Indented);

                    await textWriter.WriteAsync(json);
                }
            }
        }
        private static PropertyRenameAndIgnoreSerializerContractResolver CreateJsonSerializerContractResolver(SchemaType schemaType)
        {
            var resolver = JsonSchema.CreateJsonSerializerContractResolver(schemaType);

            if (schemaType == SchemaType.Swagger2)
            {
                resolver.IgnoreProperty(typeof(OpenApiDocument), "openapi");
                resolver.IgnoreProperty(typeof(OpenApiDocument), "servers");
                resolver.IgnoreProperty(typeof(OpenApiParameter), "title");

                // TODO: Use rename for not mapped properties!
                resolver.IgnoreProperty(typeof(OpenApiPathItem), "summary");
                resolver.IgnoreProperty(typeof(OpenApiPathItem), "description");
                resolver.IgnoreProperty(typeof(OpenApiPathItem), "servers");

                resolver.IgnoreProperty(typeof(OpenApiOperation), "callbacks");
                resolver.IgnoreProperty(typeof(OpenApiOperation), "servers");
                resolver.IgnoreProperty(typeof(OpenApiOperation), "requestBody");

                resolver.IgnoreProperty(typeof(OpenApiDocument), "components");
                resolver.IgnoreProperty(typeof(OpenApiParameter), "examples");
                resolver.IgnoreProperty(typeof(OpenApiParameter), "x-position");

                resolver.IgnoreProperty(typeof(OpenApiResponse), "content");
                resolver.IgnoreProperty(typeof(OpenApiResponse), "links");

                resolver.IgnoreProperty(typeof(OpenApiSecurityScheme), "scheme");
                resolver.IgnoreProperty(typeof(OpenApiSecurityScheme), "bearerFormat");
                resolver.IgnoreProperty(typeof(OpenApiSecurityScheme), "openIdConnectUrl");
                resolver.IgnoreProperty(typeof(OpenApiSecurityScheme), "flows");
            }
            else if (schemaType == SchemaType.OpenApi3)
            {
                resolver.IgnoreProperty(typeof(OpenApiDocument), "swagger");

                resolver.IgnoreProperty(typeof(OpenApiDocument), "host");
                resolver.IgnoreProperty(typeof(OpenApiDocument), "basePath");
                resolver.IgnoreProperty(typeof(OpenApiDocument), "schemes");

                resolver.IgnoreProperty(typeof(OpenApiDocument), "consumes");
                resolver.IgnoreProperty(typeof(OpenApiDocument), "produces");

                resolver.IgnoreProperty(typeof(OpenApiOperation), "schemes");
                resolver.IgnoreProperty(typeof(OpenApiOperation), "consumes");
                resolver.IgnoreProperty(typeof(OpenApiOperation), "produces");

                //resolver.IgnoreProperty(typeof(SwaggerParameter), "x-nullable");

                //resolver.IgnoreProperty(typeof(SwaggerResponse), "consumes"); => TODO map to response.content
                //resolver.IgnoreProperty(typeof(SwaggerResponse), "produces");

                resolver.IgnoreProperty(typeof(OpenApiDocument), "definitions");
                resolver.IgnoreProperty(typeof(OpenApiDocument), "parameters");
                resolver.IgnoreProperty(typeof(OpenApiDocument), "responses");
                resolver.IgnoreProperty(typeof(OpenApiDocument), "securityDefinitions");

                resolver.IgnoreProperty(typeof(OpenApiResponse), "schema");
                resolver.IgnoreProperty(typeof(OpenApiResponse), "examples");
                resolver.IgnoreProperty(typeof(OpenApiResponse), "x-nullable");

                resolver.IgnoreProperty(typeof(OpenApiSecurityScheme), "flow");
                resolver.IgnoreProperty(typeof(OpenApiSecurityScheme), "authorizationUrl");
                resolver.IgnoreProperty(typeof(OpenApiSecurityScheme), "tokenUrl");
                resolver.IgnoreProperty(typeof(OpenApiSecurityScheme), "scopes");
            }
            else
            {
                throw new ArgumentException("The given schema type is not supported.");
            }

            return(resolver);
        }