Skip to content

Commit c4fb402

Browse files
authored
Fix sorting of #/components/schemas/* (#50)
1 parent cef7685 commit c4fb402

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/main/java/io/openapitools/swagger/OpenAPISorter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private static void sortComponents(Components components) {
6161
return;
6262
}
6363

64-
sortSchemas(components.getSchemas());
64+
components.setSchemas(sortSchemas(components.getSchemas()));
6565

6666
components.setResponses(createSorted(components.getResponses()));
6767
components.setParameters(createSorted(components.getParameters()));

src/test/java/io/openapitools/swagger/OpenApiSorterTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ public class OpenApiSorterTest {
2222

2323
@Test
2424
public void testSort() {
25-
ObjectSchema schema = new ObjectSchema();
26-
schema.addProperties("s2", new StringSchema());
27-
schema.addProperties("s1", new StringSchema());
25+
ObjectSchema schema1 = new ObjectSchema();
26+
schema1.addProperties("s1-2", new StringSchema());
27+
schema1.addProperties("s1-1", new StringSchema());
28+
29+
ObjectSchema schema2 = new ObjectSchema();
30+
schema2.addProperties("s2-2", new StringSchema());
31+
schema2.addProperties("s2-1", new StringSchema());
2832

2933
Components components = new Components()
30-
.addSchemas("s1", schema)
34+
.addSchemas("s2", schema2)
35+
.addSchemas("s1", schema1)
3136
.addResponses("k2", new ApiResponse())
3237
.addResponses("k1", new ApiResponse())
3338
.addParameters("k2", new Parameter())
@@ -55,9 +60,13 @@ public void testSort() {
5560

5661
api = OpenAPISorter.sort(api);
5762

58-
assertEquals("s1", api.getComponents().getSchemas()
63+
assertEquals("s1-1", api.getComponents().getSchemas()
64+
.values().stream()
65+
.findFirst().get().getProperties()
66+
.keySet().stream().findFirst().get());
67+
assertEquals("s2-1", api.getComponents().getSchemas()
5968
.values().stream()
60-
.findAny().get().getProperties()
69+
.skip(1).findFirst().get().getProperties()
6170
.keySet().stream().findFirst().get());
6271
assertEquals("k1", api.getComponents().getResponses().keySet().stream().findFirst().get());
6372
assertEquals("k1", api.getComponents().getParameters().keySet().stream().findFirst().get());

0 commit comments

Comments
 (0)