Skip to content

Commit 04b52e9

Browse files
Sort required properties of schemas
Restore sorting of `required` in schemas.
1 parent 5b39b1f commit 04b52e9

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/Swashbuckle.AspNetCore.Annotations/AnnotationsSchemaFilter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ private static void ApplySchemaAttribute(IOpenApiSchema schema, SwaggerSchemaAtt
112112
}
113113
}
114114

115-
if (schemaAttribute.Required is { } required)
115+
if (schemaAttribute.Required is { Length: > 1 } required)
116116
{
117-
concrete.Required = [.. required];
117+
concrete.Required = [.. new SortedSet<string>(required)];
118118
}
119119

120120
if (schemaAttribute.Title is { } title)

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public async Task<OpenApiDocument> GetSwaggerAsync(
7070
filter.Apply(document, filterContext);
7171
}
7272

73-
SortSchemas(document);
73+
SortDocument(document);
7474

7575
return document;
7676
}
@@ -102,7 +102,7 @@ public OpenApiDocument GetSwagger(string documentName, string host = null, strin
102102
filter.Apply(document, filterContext);
103103
}
104104

105-
SortSchemas(document);
105+
SortDocument(document);
106106

107107
return document;
108108
}
@@ -129,11 +129,24 @@ public OpenApiDocument GetSwagger(string documentName, string host = null, strin
129129

130130
public IList<string> GetDocumentNames() => [.. _options.SwaggerDocs.Keys];
131131

132-
private void SortSchemas(OpenApiDocument document)
132+
private void SortDocument(OpenApiDocument document)
133133
{
134134
document.Components.Schemas =
135135
new SortedDictionary<string, IOpenApiSchema>(document.Components.Schemas, _options.SchemaComparer)
136136
.ToDictionary((k) => k.Key, (v) => v.Value);
137+
138+
foreach (var schema in document.Components.Schemas.Values)
139+
{
140+
SortSchemas(schema);
141+
}
142+
143+
static void SortSchemas(IOpenApiSchema schema)
144+
{
145+
if (schema is OpenApiSchema { Required.Count: > 1 } concrete)
146+
{
147+
concrete.Required = [.. new SortedSet<string>(concrete.Required)];
148+
}
149+
}
137150
}
138151

139152
private (DocumentFilterContext, OpenApiDocument) GetSwaggerDocumentWithoutPaths(string documentName, string host = null, string basePath = null)

0 commit comments

Comments
 (0)