RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.73k stars 1.29k forks source link

OpenAPIDocument with schemaType=OpenApi3 uses Newtonsoft.Json static serializer with default settings #4793

Open Feoni4 opened 7 months ago

Feoni4 commented 7 months ago

OpenAPIDocument with schemaType=OpenApi3 uses Newtonsoft.Json static serializer with default settings. Expected behavior: Serialization is performed with System.Text.Json serializer and Mvc.JsonOptions \ Http.Json.JsonOptions are used during serialization. Or provide a way to configure the serialization settings - so we can add custom JsonConverter's, null value handling, etc. Also it will help to get expected serialized content for custom Examples for operation responses \ request parameters.

OpenApiDocument.cs

        /// <summary>Converts the description object to JSON.</summary>
        /// <param name="schemaType">The schema type.</param>
        /// <param name="formatting">The formatting.</param>
        /// <returns>The JSON string.</returns>
        public string ToJson(SchemaType schemaType, Formatting formatting)
        {
            GenerateOperationIds();

            var contractResolver = GetJsonSerializerContractResolver(schemaType);
            return JsonSchemaSerialization.ToJson(this, schemaType, contractResolver, formatting);
        }

JsonSchemaSerialization.cs

        public static string ToJson(object obj, SchemaType schemaType, IContractResolver contractResolver, Formatting formatting)
        {
            IsWriting = false;
            CurrentSchemaType = schemaType;

            JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(obj, false, contractResolver);

            IsWriting = false;
            CurrentSerializerSettings = new JsonSerializerSettings
            {
                ContractResolver = contractResolver
            };

            var json = JsonConvert.SerializeObject(obj, formatting, CurrentSerializerSettings);

            CurrentSerializerSettings = null;
            CurrentSchemaType = SchemaType.JsonSchema;

            return json;
        }