diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs index 073f63c04f..6f2e72226c 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs @@ -23,7 +23,7 @@ public DataContract GetDataContractForType(Type type) { return DataContract.ForDynamic( underlyingType: type, - jsonConverter: JsonConverterFunc); + jsonConverter: (value) => JsonConverterFunc(value, type)); } if (PrimitiveTypesAndFormats.ContainsKey(type)) @@ -34,7 +34,7 @@ public DataContract GetDataContractForType(Type type) underlyingType: type, dataType: primitiveTypeAndFormat.Item1, dataFormat: primitiveTypeAndFormat.Item2, - jsonConverter: JsonConverterFunc); + jsonConverter: (value) => JsonConverterFunc(value, type)); } if (type.IsEnum) @@ -43,7 +43,7 @@ public DataContract GetDataContractForType(Type type) //Test to determine if the serializer will treat as string var serializeAsString = (enumValues.Length > 0) - && JsonConverterFunc(enumValues.GetValue(0)).StartsWith("\""); + && JsonConverterFunc(enumValues.GetValue(0), type).StartsWith("\""); var primitiveTypeAndFormat = serializeAsString ? PrimitiveTypesAndFormats[typeof(string)] @@ -53,7 +53,7 @@ public DataContract GetDataContractForType(Type type) underlyingType: type, dataType: primitiveTypeAndFormat.Item1, dataFormat: primitiveTypeAndFormat.Item2, - jsonConverter: JsonConverterFunc); + jsonConverter: (value) => JsonConverterFunc(value, type)); } if (IsSupportedDictionary(type, out Type keyType, out Type valueType)) @@ -62,7 +62,7 @@ public DataContract GetDataContractForType(Type type) underlyingType: type, valueType: valueType, keys: null, // STJ doesn't currently support dictionaries with enum key types - jsonConverter: JsonConverterFunc); + jsonConverter: (value) => JsonConverterFunc(value, type)); } if (IsSupportedCollection(type, out Type itemType)) @@ -70,19 +70,19 @@ public DataContract GetDataContractForType(Type type) return DataContract.ForArray( underlyingType: type, itemType: itemType, - jsonConverter: JsonConverterFunc); + jsonConverter: (value) => JsonConverterFunc(value, type)); } return DataContract.ForObject( underlyingType: type, properties: GetDataPropertiesFor(type, out Type extensionDataType), extensionDataType: extensionDataType, - jsonConverter: JsonConverterFunc); + jsonConverter: (value) => JsonConverterFunc(value, type)); } - private string JsonConverterFunc(object value) + private string JsonConverterFunc(object value, Type type) { - return JsonSerializer.Serialize(value, _serializerOptions); + return JsonSerializer.Serialize(value, type, _serializerOptions); } public bool IsSupportedDictionary(Type type, out Type keyType, out Type valueType)