Skip to content

Commit

Permalink
Fix sorting of #/components/schemas/* (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddewie committed Jul 3, 2020
1 parent cef7685 commit c4fb402
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/openapitools/swagger/OpenAPISorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static void sortComponents(Components components) {
return;
}

sortSchemas(components.getSchemas());
components.setSchemas(sortSchemas(components.getSchemas()));

components.setResponses(createSorted(components.getResponses()));
components.setParameters(createSorted(components.getParameters()));
Expand Down
21 changes: 15 additions & 6 deletions src/test/java/io/openapitools/swagger/OpenApiSorterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ public class OpenApiSorterTest {

@Test
public void testSort() {
ObjectSchema schema = new ObjectSchema();
schema.addProperties("s2", new StringSchema());
schema.addProperties("s1", new StringSchema());
ObjectSchema schema1 = new ObjectSchema();
schema1.addProperties("s1-2", new StringSchema());
schema1.addProperties("s1-1", new StringSchema());

ObjectSchema schema2 = new ObjectSchema();
schema2.addProperties("s2-2", new StringSchema());
schema2.addProperties("s2-1", new StringSchema());

Components components = new Components()
.addSchemas("s1", schema)
.addSchemas("s2", schema2)
.addSchemas("s1", schema1)
.addResponses("k2", new ApiResponse())
.addResponses("k1", new ApiResponse())
.addParameters("k2", new Parameter())
Expand Down Expand Up @@ -55,9 +60,13 @@ public void testSort() {

api = OpenAPISorter.sort(api);

assertEquals("s1", api.getComponents().getSchemas()
assertEquals("s1-1", api.getComponents().getSchemas()
.values().stream()
.findFirst().get().getProperties()
.keySet().stream().findFirst().get());
assertEquals("s2-1", api.getComponents().getSchemas()
.values().stream()
.findAny().get().getProperties()
.skip(1).findFirst().get().getProperties()
.keySet().stream().findFirst().get());
assertEquals("k1", api.getComponents().getResponses().keySet().stream().findFirst().get());
assertEquals("k1", api.getComponents().getParameters().keySet().stream().findFirst().get());
Expand Down

0 comments on commit c4fb402

Please sign in to comment.