Skip to content

Commit e4896bd

Browse files
Fix invalid schema generation
The `nullable` property should not be emitted when `type` is not. Found via `https://apinotes.io/openapi-validator`. Relates to #3649 and #3683.
1 parent 49255e6 commit e4896bd

8 files changed

Lines changed: 13 additions & 13 deletions

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

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

52+
bool usingAllOf = false;
53+
5254
if (_generatorOptions.UseAllOfToExtendReferenceSchemas && schema is OpenApiSchemaReference reference)
5355
{
5456
schema = new OpenApiSchema() { AllOf = [reference] };
57+
usingAllOf = true;
5558
}
5659

5760
if (schema is OpenApiSchema concrete)
@@ -63,9 +66,12 @@ private IOpenApiSchema GenerateSchemaForMember(
6366
{
6467
var requiredAttribute = customAttributes.OfType<RequiredAttribute>().FirstOrDefault();
6568

66-
var nullable = IsNullable(requiredAttribute, dataProperty, memberInfo);
67-
68-
SetNullable(concrete, nullable);
69+
// "nullable" cannot be used without "type"
70+
if (!usingAllOf)
71+
{
72+
var nullable = IsNullable(requiredAttribute, dataProperty, memberInfo);
73+
SetNullable(concrete, nullable);
74+
}
6975

7076
concrete.ReadOnly = dataProperty.IsReadOnly;
7177
concrete.WriteOnly = dataProperty.IsWriteOnly;

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": "DF0ACFCDC1F5CFB97A2DFB0E7A7BB004B889E5742C1925E797BC2A6B816489D5C9A4A8413CB3795FA45E7589D0E04D80856F3558CD656308C19DBDA9446A0712",
2+
"descriptionHash": "6220470D2DBDA8D33A3A26AE0D99FD08E8CA025558CE7AF3EFA771BEE7F540049DFB9E14E28367BD30CC488F5A918670E40CF226C0C825CC163EDBFFA1CCE766",
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/VerifyTests.Swagger_IsValidJson_No_Startup_entryPointType=MinimalAppWithNullableEnums.Program_swaggerRequestUri=v1.DotNet10_0.verified.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"type": "object",
4949
"properties": {
5050
"child": {
51-
"type": "null",
5251
"allOf": [
5352
{
5453
"$ref": "#/components/schemas/Child"

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"type": "object",
4949
"properties": {
5050
"child": {
51-
"type": "null",
5251
"allOf": [
5352
{
5453
"$ref": "#/components/schemas/Child"

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"type": "object",
4949
"properties": {
5050
"child": {
51-
"type": "null",
5251
"allOf": [
5352
{
5453
"$ref": "#/components/schemas/Child"

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,7 @@
420420
"$ref": "#/components/schemas/TodoPriority"
421421
}
422422
],
423-
"description": "Gets or sets the optional priority of the Todo item.",
424-
"nullable": true
423+
"description": "Gets or sets the optional priority of the Todo item."
425424
}
426425
},
427426
"additionalProperties": false,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,7 @@
418418
"$ref": "#/components/schemas/TodoPriority"
419419
}
420420
],
421-
"description": "Gets or sets the optional priority of the Todo item.",
422-
"nullable": true
421+
"description": "Gets or sets the optional priority of the Todo item."
423422
}
424423
},
425424
"additionalProperties": false,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,7 @@
418418
"$ref": "#/components/schemas/TodoPriority"
419419
}
420420
],
421-
"description": "Gets or sets the optional priority of the Todo item.",
422-
"nullable": true
421+
"description": "Gets or sets the optional priority of the Todo item."
423422
}
424423
},
425424
"additionalProperties": false,

0 commit comments

Comments
 (0)