Skip to content

Commit c361462

Browse files
Revert #3377
Manually revert changes from #3377. Resolves #3425.
1 parent b28e9c8 commit c361462

14 files changed

+28
-146
lines changed

src/Swashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ public DataContract GetDataContractForType(Type type)
2222
jsonConverter: JsonConverterFunc);
2323
}
2424

25-
var jsonContract = _contractResolver.ResolveContract(type);
25+
var jsonContract = _contractResolver.ResolveContract(effectiveType);
2626

27-
var effectiveUnderlyingType = Nullable.GetUnderlyingType(jsonContract.UnderlyingType) ?? jsonContract.UnderlyingType;
28-
29-
if (jsonContract is JsonPrimitiveContract && !effectiveUnderlyingType.IsEnum)
27+
if (jsonContract is JsonPrimitiveContract && !jsonContract.UnderlyingType.IsEnum)
3028
{
31-
if (!PrimitiveTypesAndFormats.TryGetValue(effectiveUnderlyingType, out var primitiveTypeAndFormat))
29+
if (!PrimitiveTypesAndFormats.TryGetValue(jsonContract.UnderlyingType, out var primitiveTypeAndFormat))
3230
{
3331
primitiveTypeAndFormat = Tuple.Create(DataType.String, (string)null);
3432
}
@@ -40,9 +38,9 @@ public DataContract GetDataContractForType(Type type)
4038
jsonConverter: JsonConverterFunc);
4139
}
4240

43-
if (jsonContract is JsonPrimitiveContract && effectiveUnderlyingType.IsEnum)
41+
if (jsonContract is JsonPrimitiveContract && jsonContract.UnderlyingType.IsEnum)
4442
{
45-
var enumValues = effectiveUnderlyingType.GetEnumValues();
43+
var enumValues = jsonContract.UnderlyingType.GetEnumValues();
4644

4745
// Test to determine if the serializer will treat as string
4846
var serializeAsString = (enumValues.Length > 0) &&
@@ -54,7 +52,7 @@ public DataContract GetDataContractForType(Type type)
5452

5553
var primitiveTypeAndFormat = serializeAsString
5654
? PrimitiveTypesAndFormats[typeof(string)]
57-
: PrimitiveTypesAndFormats[effectiveUnderlyingType.GetEnumUnderlyingType()];
55+
: PrimitiveTypesAndFormats[jsonContract.UnderlyingType.GetEnumUnderlyingType()];
5856

5957
return DataContract.ForPrimitive(
6058
underlyingType: jsonContract.UnderlyingType,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public DataContract GetDataContractForType(Type type)
4949
primitiveTypeAndFormat = PrimitiveTypesAndFormats[exampleType];
5050

5151
return DataContract.ForPrimitive(
52-
underlyingType: type,
52+
underlyingType: effectiveType,
5353
dataType: primitiveTypeAndFormat.Item1,
5454
dataFormat: primitiveTypeAndFormat.Item2,
5555
jsonConverter: (value) => JsonConverterFunc(value, type));

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ private OpenApiSchema GenerateSchemaForMember(
5454
MemberInfo memberInfo,
5555
DataProperty dataProperty = null)
5656
{
57-
if (dataProperty != null)
58-
{
59-
var customAttributes = memberInfo.GetInlineAndMetadataAttributes();
60-
61-
var requiredAttribute = customAttributes.OfType<RequiredAttribute>().FirstOrDefault();
62-
63-
if (!IsNullable(requiredAttribute, dataProperty, memberInfo))
64-
{
65-
modelType = Nullable.GetUnderlyingType(modelType) ?? modelType;
66-
}
67-
}
68-
6957
var dataContract = GetDataContractFor(modelType);
7058

7159
var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts)
@@ -284,7 +272,7 @@ private OpenApiSchema GenerateConcreteSchema(DataContract dataContract, SchemaRe
284272
case DataType.String:
285273
{
286274
schemaFactory = () => CreatePrimitiveSchema(dataContract);
287-
returnAsReference = (Nullable.GetUnderlyingType(dataContract.UnderlyingType) ?? dataContract.UnderlyingType).IsEnum && !_generatorOptions.UseInlineDefinitionsForEnums;
275+
returnAsReference = dataContract.UnderlyingType.IsEnum && !_generatorOptions.UseInlineDefinitionsForEnums;
288276
break;
289277
}
290278

@@ -350,18 +338,12 @@ private static OpenApiSchema CreatePrimitiveSchema(DataContract dataContract)
350338
}
351339
#pragma warning restore CS0618 // Type or member is obsolete
352340

353-
var underlyingType = Nullable.GetUnderlyingType(dataContract.UnderlyingType) ?? dataContract.UnderlyingType;
341+
var underlyingType = dataContract.UnderlyingType;
354342

355343
if (underlyingType.IsEnum)
356344
{
357345
var enumValues = underlyingType.GetEnumValues().Cast<object>();
358346

359-
if (dataContract.UnderlyingType != underlyingType)
360-
{
361-
schema.Nullable = true;
362-
enumValues = enumValues.Append(null);
363-
}
364-
365347
schema.Enum = [.. enumValues
366348
.Select(value => dataContract.JsonConverter(value))
367349
.Distinct()
@@ -469,7 +451,7 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
469451
continue;
470452
}
471453

472-
var memberType = dataProperty.IsNullable ? dataProperty.MemberType : (Nullable.GetUnderlyingType(dataProperty.MemberType) ?? dataProperty.MemberType);
454+
var memberType = dataProperty.MemberType;
473455

474456
schema.Properties[dataProperty.Name] = (dataProperty.MemberInfo != null)
475457
? GenerateSchemaForMember(memberType, schemaRepository, dataProperty.MemberInfo, dataProperty)

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ apiParameter.Type is not null &&
598598

599599
var schema = (type != null)
600600
? GenerateSchema(
601-
isRequired ? (Nullable.GetUnderlyingType(type) ?? type) : type,
601+
type,
602602
schemaRepository,
603603
apiParameter.PropertyInfo(),
604604
apiParameter.ParameterInfo(),

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.DotNet8_0.verified.txt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@
14911491
"$ref": "#/components/schemas/ProductStatus"
14921492
},
14931493
"status2": {
1494-
"$ref": "#/components/schemas/ProductStatusNullable"
1494+
"$ref": "#/components/schemas/ProductStatus"
14951495
},
14961496
"price": {
14971497
"maximum": 122.9,
@@ -1520,17 +1520,6 @@
15201520
"type": "integer",
15211521
"format": "int32"
15221522
},
1523-
"ProductStatusNullable": {
1524-
"enum": [
1525-
0,
1526-
1,
1527-
2,
1528-
null
1529-
],
1530-
"type": "integer",
1531-
"format": "int32",
1532-
"nullable": true
1533-
},
15341523
"Promotion": {
15351524
"type": "object",
15361525
"properties": {

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.DotNet9_0.verified.txt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@
14911491
"$ref": "#/components/schemas/ProductStatus"
14921492
},
14931493
"status2": {
1494-
"$ref": "#/components/schemas/ProductStatusNullable"
1494+
"$ref": "#/components/schemas/ProductStatus"
14951495
},
14961496
"price": {
14971497
"maximum": 122.9,
@@ -1520,17 +1520,6 @@
15201520
"type": "integer",
15211521
"format": "int32"
15221522
},
1523-
"ProductStatusNullable": {
1524-
"enum": [
1525-
0,
1526-
1,
1527-
2,
1528-
null
1529-
],
1530-
"type": "integer",
1531-
"format": "int32",
1532-
"nullable": true
1533-
},
15341523
"Promotion": {
15351524
"type": "object",
15361525
"properties": {

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/VerifyTests.Swagger_IsValidJson_No_Startup_entryPointType=MvcWithNullable.Program_swaggerRequestUri=v1.DotNet8_0.verified.txt

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"schema": {
1818
"allOf": [
1919
{
20-
"$ref": "#/components/schemas/LogLevelNullable"
20+
"$ref": "#/components/schemas/LogLevel"
2121
}
2222
],
2323
"default": 4
@@ -73,21 +73,6 @@
7373
],
7474
"type": "integer",
7575
"format": "int32"
76-
},
77-
"LogLevelNullable": {
78-
"enum": [
79-
0,
80-
1,
81-
2,
82-
3,
83-
4,
84-
5,
85-
6,
86-
null
87-
],
88-
"type": "integer",
89-
"format": "int32",
90-
"nullable": true
9176
}
9277
}
9378
}

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/VerifyTests.Swagger_IsValidJson_No_Startup_entryPointType=MvcWithNullable.Program_swaggerRequestUri=v1.DotNet9_0.verified.txt

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"schema": {
1818
"allOf": [
1919
{
20-
"$ref": "#/components/schemas/LogLevelNullable"
20+
"$ref": "#/components/schemas/LogLevel"
2121
}
2222
],
2323
"default": 4
@@ -73,21 +73,6 @@
7373
],
7474
"type": "integer",
7575
"format": "int32"
76-
},
77-
"LogLevelNullable": {
78-
"enum": [
79-
0,
80-
1,
81-
2,
82-
3,
83-
4,
84-
5,
85-
6,
86-
null
87-
],
88-
"type": "integer",
89-
"format": "int32",
90-
"nullable": true
9176
}
9277
}
9378
}

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/VerifyTests.Swagger_IsValidJson_No_Startup_entryPointType=WebApi.Program_swaggerRequestUri=v1.DotNet8_0.verified.txt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@
379379
"name": "paramNine",
380380
"in": "query",
381381
"schema": {
382-
"$ref": "#/components/schemas/DateTimeKindNullable"
382+
"$ref": "#/components/schemas/DateTimeKind"
383383
}
384384
},
385385
{
@@ -946,7 +946,7 @@
946946
"format": "time"
947947
},
948948
"paramNine": {
949-
"$ref": "#/components/schemas/DateTimeKindNullable"
949+
"$ref": "#/components/schemas/DateTimeKind"
950950
},
951951
"paramTen": {
952952
"$ref": "#/components/schemas/DateTimeKind"
@@ -995,17 +995,6 @@
995995
"type": "integer",
996996
"format": "int32"
997997
},
998-
"DateTimeKindNullable": {
999-
"enum": [
1000-
0,
1001-
1,
1002-
2,
1003-
null
1004-
],
1005-
"type": "integer",
1006-
"format": "int32",
1007-
"nullable": true
1008-
},
1009998
"Fruit": {
1010999
"type": "object",
10111000
"properties": {

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/VerifyTests.Swagger_IsValidJson_No_Startup_entryPointType=WebApi.Program_swaggerRequestUri=v1.DotNet9_0.verified.txt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@
379379
"name": "paramNine",
380380
"in": "query",
381381
"schema": {
382-
"$ref": "#/components/schemas/DateTimeKindNullable"
382+
"$ref": "#/components/schemas/DateTimeKind"
383383
}
384384
},
385385
{
@@ -946,7 +946,7 @@
946946
"format": "time"
947947
},
948948
"paramNine": {
949-
"$ref": "#/components/schemas/DateTimeKindNullable"
949+
"$ref": "#/components/schemas/DateTimeKind"
950950
},
951951
"paramTen": {
952952
"$ref": "#/components/schemas/DateTimeKind"
@@ -995,17 +995,6 @@
995995
"type": "integer",
996996
"format": "int32"
997997
},
998-
"DateTimeKindNullable": {
999-
"enum": [
1000-
0,
1001-
1,
1002-
2,
1003-
null
1004-
],
1005-
"type": "integer",
1006-
"format": "int32",
1007-
"nullable": true
1008-
},
1009998
"Fruit": {
1010999
"type": "object",
10111000
"properties": {

0 commit comments

Comments
 (0)