From 30f7c11c57f5c81b20da093f0a4f22a32ba6bf9e Mon Sep 17 00:00:00 2001 From: Jans Rautenbach Date: Mon, 18 Sep 2023 12:35:31 +0200 Subject: [PATCH 1/2] Sort properties --- .../github/kongchen/swagger/docgen/Utils.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/kongchen/swagger/docgen/Utils.java b/src/main/java/com/github/kongchen/swagger/docgen/Utils.java index 9c9d87bee..907730798 100644 --- a/src/main/java/com/github/kongchen/swagger/docgen/Utils.java +++ b/src/main/java/com/github/kongchen/swagger/docgen/Utils.java @@ -8,6 +8,7 @@ import io.swagger.models.Response; import io.swagger.models.Swagger; import io.swagger.models.Tag; +import io.swagger.models.properties.Property; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -87,9 +88,7 @@ public static void sortSwagger(Swagger swagger) throws GenerateException { //reorder definitions if (swagger.getDefinitions() != null) { - TreeMap defs = new TreeMap(); - defs.putAll(swagger.getDefinitions()); - swagger.setDefinitions(defs); + swagger.setDefinitions(sortDefinitions(swagger.getDefinitions())); } // order the tags @@ -103,6 +102,22 @@ public int compare(final Tag a, final Tag b) { } + /** + * Produce a sorted map of models, where the models are sorted by name, and the properties within each model are also + * sorted by name. + * @apiNote The map of models argument is not mutated, however the properties of each model is mutated (re-ordered). + * @param definitions to sort + * @return A new sorted map of models/definitions. + */ + private static Map sortDefinitions(Map definitions){ + for (Model model : definitions.values()) { + // Sort the properties by placing them in a TreeMap + model.setProperties(new TreeMap<>(model.getProperties())); + } + // Sort the definitions by placing them in a TreeMap + return new TreeMap<>(definitions); + } + private static void sortResponses(Path path, String method) throws GenerateException { try { Method m = Path.class.getDeclaredMethod("get" + method); From db0ef6fa47c865903fc5558e3136df4c9e115ed0 Mon Sep 17 00:00:00 2001 From: Jans Rautenbach Date: Mon, 18 Sep 2023 15:19:56 +0200 Subject: [PATCH 2/2] Testing the sortSwagger() utils method --- .../swagger/docgen/TestUtilsClass.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/java/com/github/kongchen/swagger/docgen/TestUtilsClass.java diff --git a/src/test/java/com/github/kongchen/swagger/docgen/TestUtilsClass.java b/src/test/java/com/github/kongchen/swagger/docgen/TestUtilsClass.java new file mode 100644 index 000000000..3182c07e3 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/docgen/TestUtilsClass.java @@ -0,0 +1,50 @@ +package com.github.kongchen.swagger.docgen; + +import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.Swagger; +import io.swagger.models.properties.FloatProperty; +import io.swagger.models.properties.IntegerProperty; +import io.swagger.models.properties.Property; +import io.swagger.models.properties.StringProperty; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.HashMap; +import java.util.Set; + +/** + * Tests for the {@link Utils} class. + */ +public class TestUtilsClass { + + @Test + public void testSortSwaggerOrderedProperties() throws GenerateException { + // Create a swagger with models and properties in arbitrary order. + Swagger swagger = new Swagger(); + + HashMap props = new HashMap<>(); + props.put("c", new StringProperty()); + props.put("b", new IntegerProperty()); + props.put("a", new FloatProperty()); + + HashMap definitions = new HashMap<>(); + definitions.put("c", new ModelImpl()); + definitions.put("b", new ModelImpl()); + definitions.put("a", new ModelImpl()); + definitions.forEach((name, defn) -> defn.setProperties(props)); + swagger.setDefinitions(definitions); + + // Action + Utils.sortSwagger(swagger); + + // Verify that all names are in order + String[] orderedNames = new String[]{"a", "b", "c"}; + Set modelNames = swagger.getDefinitions().keySet(); + Assert.assertEquals(modelNames.toArray(), orderedNames); + for (String modelName : modelNames) { + Set propNames = swagger.getDefinitions().get(modelName).getProperties().keySet(); + Assert.assertEquals(propNames.toArray(), orderedNames); + } + } +}