Skip to content

Commit 49255e6

Browse files
Revert AllOf behaviour changes for nullable
Reverts cdebc52. Fixes #3687.
1 parent a71efa0 commit 49255e6

14 files changed

Lines changed: 95 additions & 51 deletions

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,9 @@ private IOpenApiSchema GenerateSchemaForMember(
4949
? GeneratePolymorphicSchema(schemaRepository, knownTypesDataContracts)
5050
: GenerateConcreteSchema(dataContract, schemaRepository);
5151

52-
bool usingAllOf = false;
53-
5452
if (_generatorOptions.UseAllOfToExtendReferenceSchemas && schema is OpenApiSchemaReference reference)
5553
{
5654
schema = new OpenApiSchema() { AllOf = [reference] };
57-
usingAllOf = true;
5855
}
5956

6057
if (schema is OpenApiSchema concrete)
@@ -68,22 +65,7 @@ private IOpenApiSchema GenerateSchemaForMember(
6865

6966
var nullable = IsNullable(requiredAttribute, dataProperty, memberInfo);
7067

71-
if (usingAllOf)
72-
{
73-
// When using AllOf to extend reference schemas, we need to adjust the schema to represent
74-
// nullability correctly as a property can't be null AND a specific type at the same time.
75-
// See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/3649.
76-
if (nullable)
77-
{
78-
concrete.OneOf = schema.AllOf;
79-
concrete.OneOf.Add(new OpenApiSchema { Type = JsonSchemaType.Null });
80-
concrete.AllOf = null;
81-
}
82-
}
83-
else
84-
{
85-
SetNullable(concrete, nullable);
86-
}
68+
SetNullable(concrete, nullable);
8769

8870
concrete.ReadOnly = dataProperty.IsReadOnly;
8971
concrete.WriteOnly = dataProperty.IsWriteOnly;

test/Swashbuckle.AspNetCore.IntegrationTests/TodoClient/Models/TodoItemModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public partial class TodoItemModel : IParsable
2727
#endif
2828
/// <summary>Gets or sets the date and time the Todo item was last updated.</summary>
2929
public DateTimeOffset? LastUpdated { get; set; }
30-
/// <summary>The priority levels for a Todo item.</summary>
30+
/// <summary>Gets or sets the optional priority of the Todo item.</summary>
3131
public global::TodoApp.Client.Models.TodoPriority? Priority { get; set; }
3232
/// <summary>Gets or sets the text of the Todo item.</summary>
3333
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER

test/Swashbuckle.AspNetCore.IntegrationTests/TodoClient/Models/UpdateTodoItemPriorityModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace TodoApp.Client.Models
1313
[global::System.CodeDom.Compiler.GeneratedCode("Kiota", "1.0.0")]
1414
public partial class UpdateTodoItemPriorityModel : IParsable
1515
{
16-
/// <summary>The priority levels for a Todo item.</summary>
16+
/// <summary>Gets or sets the new priority of the Todo item.</summary>
1717
public global::TodoApp.Client.Models.TodoPriority? Priority { get; set; }
1818
/// <summary>
1919
/// Creates a new instance of the appropriate class based on discriminator value

test/Swashbuckle.AspNetCore.IntegrationTests/TodoClient/kiota-lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"descriptionHash": "68C747E40510EFE3E3E4245BE5DAB465900ED6EE0AD4B08CFE317290C0A20297E820040CD2354973E7D07366B2320504CD291FC77EFA34BFB805254D2A594BF6",
2+
"descriptionHash": "DF0ACFCDC1F5CFB97A2DFB0E7A7BB004B889E5742C1925E797BC2A6B816489D5C9A4A8413CB3795FA45E7589D0E04D80856F3558CD656308C19DBDA9446A0712",
33
"descriptionLocation": "../snapshots/VerifyTests.Swagger_IsValidJson_No_Startup_entryPointType=TodoApp.Program_swaggerRequestUri=v1.DotNet10_0.verified.txt",
44
"lockFileVersion": "1.0.0",
55
"kiotaVersion": "1.29.0",

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/CodeGenerationTests.VerifyKiotaTodoAppClient/Models/TodoItemModel.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public partial class TodoItemModel : IParsable
2727
#endif
2828
/// <summary>Gets or sets the date and time the Todo item was last updated.</summary>
2929
public DateTimeOffset? LastUpdated { get; set; }
30-
/// <summary>The priority levels for a Todo item.</summary>
30+
/// <summary>Gets or sets the optional priority of the Todo item.</summary>
3131
public global::TodoApp.Client.Models.TodoPriority? Priority { get; set; }
3232
/// <summary>Gets or sets the text of the Todo item.</summary>
3333
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/CodeGenerationTests.VerifyKiotaTodoAppClient/Models/UpdateTodoItemPriorityModel.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace TodoApp.Client.Models
1313
[global::System.CodeDom.Compiler.GeneratedCode("Kiota", "1.0.0")]
1414
public partial class UpdateTodoItemPriorityModel : IParsable
1515
{
16-
/// <summary>The priority levels for a Todo item.</summary>
16+
/// <summary>Gets or sets the new priority of the Todo item.</summary>
1717
public global::TodoApp.Client.Models.TodoPriority? Priority { get; set; }
1818
/// <summary>
1919
/// Creates a new instance of the appropriate class based on discriminator value

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/VerifyTests.Swagger_IsValidJson_No_Startup_entryPointType=MinimalAppWithNullableEnums.Program_swaggerRequestUri=v1.DotNet10_0.verified.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@
4848
"type": "object",
4949
"properties": {
5050
"child": {
51-
"oneOf": [
51+
"type": "null",
52+
"allOf": [
5253
{
5354
"$ref": "#/components/schemas/Child"
54-
},
55-
{
56-
"type": "null"
5755
}
5856
]
5957
},

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@
4848
"type": "object",
4949
"properties": {
5050
"child": {
51-
"oneOf": [
51+
"type": "null",
52+
"allOf": [
5253
{
5354
"$ref": "#/components/schemas/Child"
54-
},
55-
{
56-
"type": "null"
5755
}
5856
]
5957
},

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@
4848
"type": "object",
4949
"properties": {
5050
"child": {
51-
"oneOf": [
51+
"type": "null",
52+
"allOf": [
5253
{
5354
"$ref": "#/components/schemas/Child"
54-
},
55-
{
56-
"type": "null"
5755
}
5856
]
5957
},

test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/VerifyTests.Swagger_IsValidJson_No_Startup_entryPointType=TodoApp.Program_swaggerRequestUri=v1.DotNet10_0.verified.txt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
3737
"content": {
3838
"application/json": {
3939
"schema": {
40-
"$ref": "#/components/schemas/CreateTodoItemModel"
40+
"allOf": [
41+
{
42+
"$ref": "#/components/schemas/CreateTodoItemModel"
43+
}
44+
],
45+
"description": "Represents the model for creating a new Todo item."
4146
}
4247
}
4348
},
@@ -221,7 +226,12 @@
221226
"content": {
222227
"application/json": {
223228
"schema": {
224-
"$ref": "#/components/schemas/UpdateTodoItemPriorityModel"
229+
"allOf": [
230+
{
231+
"$ref": "#/components/schemas/UpdateTodoItemPriorityModel"
232+
}
233+
],
234+
"description": "Represents the model for updating the priority of a Todo item."
225235
}
226236
}
227237
},
@@ -405,7 +415,13 @@
405415
"format": "date-time"
406416
},
407417
"priority": {
408-
"$ref": "#/components/schemas/TodoPriority"
418+
"allOf": [
419+
{
420+
"$ref": "#/components/schemas/TodoPriority"
421+
}
422+
],
423+
"description": "Gets or sets the optional priority of the Todo item.",
424+
"nullable": true
409425
}
410426
},
411427
"additionalProperties": false,
@@ -438,7 +454,12 @@
438454
"type": "object",
439455
"properties": {
440456
"priority": {
441-
"$ref": "#/components/schemas/TodoPriority"
457+
"allOf": [
458+
{
459+
"$ref": "#/components/schemas/TodoPriority"
460+
}
461+
],
462+
"description": "Gets or sets the new priority of the Todo item."
442463
}
443464
},
444465
"additionalProperties": false,

0 commit comments

Comments
 (0)