Skip to content

Commit 07d48a5

Browse files
Fix schemas for nullable properties
Fix incorrect handling of schemas for nullable properties. Resolves #3013.
1 parent 3fa1702 commit 07d48a5

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public DataContract GetDataContractForType(Type type)
9696

9797
return DataContract.ForObject(
9898
underlyingType: effectiveType,
99-
properties: GetDataPropertiesFor(type, out Type extensionDataType),
99+
properties: GetDataPropertiesFor(effectiveType, out Type extensionDataType),
100100
extensionDataType: extensionDataType,
101101
jsonConverter: (value) => JsonConverterFunc(value, effectiveType));
102102
}

test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_Basic_DotNet_6.verified.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,36 @@
726726
x-purpose: test
727727
}
728728
},
729+
/Issue3013/Get: {
730+
get: {
731+
tags: [
732+
Issue3013
733+
],
734+
responses: {
735+
200: {
736+
description: OK,
737+
content: {
738+
text/plain: {
739+
schema: {
740+
$ref: #/components/schemas/TestResponse
741+
}
742+
},
743+
application/json: {
744+
schema: {
745+
$ref: #/components/schemas/TestResponse
746+
}
747+
},
748+
text/json: {
749+
schema: {
750+
$ref: #/components/schemas/TestResponse
751+
}
752+
}
753+
}
754+
}
755+
},
756+
x-purpose: test
757+
}
758+
},
729759
/promotions: {
730760
get: {
731761
tags: [
@@ -1381,6 +1411,29 @@
13811411
},
13821412
additionalProperties: false
13831413
},
1414+
TestResponse: {
1415+
type: object,
1416+
properties: {
1417+
foo: {
1418+
$ref: #/components/schemas/TestStruct
1419+
}
1420+
},
1421+
additionalProperties: false
1422+
},
1423+
TestStruct: {
1424+
type: object,
1425+
properties: {
1426+
a: {
1427+
type: integer,
1428+
format: int32
1429+
},
1430+
b: {
1431+
type: integer,
1432+
format: int32
1433+
}
1434+
},
1435+
additionalProperties: false
1436+
},
13841437
Transaction: {
13851438
required: [
13861439
amount

test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_Basic_DotNet_8.verified.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,36 @@
726726
x-purpose: test
727727
}
728728
},
729+
/Issue3013/Get: {
730+
get: {
731+
tags: [
732+
Issue3013
733+
],
734+
responses: {
735+
200: {
736+
description: OK,
737+
content: {
738+
text/plain: {
739+
schema: {
740+
$ref: #/components/schemas/TestResponse
741+
}
742+
},
743+
application/json: {
744+
schema: {
745+
$ref: #/components/schemas/TestResponse
746+
}
747+
},
748+
text/json: {
749+
schema: {
750+
$ref: #/components/schemas/TestResponse
751+
}
752+
}
753+
}
754+
}
755+
},
756+
x-purpose: test
757+
}
758+
},
729759
/promotions: {
730760
get: {
731761
tags: [
@@ -1381,6 +1411,29 @@
13811411
},
13821412
additionalProperties: false
13831413
},
1414+
TestResponse: {
1415+
type: object,
1416+
properties: {
1417+
foo: {
1418+
$ref: #/components/schemas/TestStruct
1419+
}
1420+
},
1421+
additionalProperties: false
1422+
},
1423+
TestStruct: {
1424+
type: object,
1425+
properties: {
1426+
a: {
1427+
type: integer,
1428+
format: int32
1429+
},
1430+
b: {
1431+
type: integer,
1432+
format: int32
1433+
}
1434+
},
1435+
additionalProperties: false
1436+
},
13841437
Transaction: {
13851438
required: [
13861439
amount

0 commit comments

Comments
 (0)