Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort properties #896

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/main/java/com/github/kongchen/swagger/docgen/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,9 +88,7 @@ public static void sortSwagger(Swagger swagger) throws GenerateException {

//reorder definitions
if (swagger.getDefinitions() != null) {
TreeMap<String, Model> defs = new TreeMap<String, Model>();
defs.putAll(swagger.getDefinitions());
swagger.setDefinitions(defs);
swagger.setDefinitions(sortDefinitions(swagger.getDefinitions()));
}

// order the tags
Expand All @@ -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<String, Model> sortDefinitions(Map<String, Model> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Property> props = new HashMap<>();
props.put("c", new StringProperty());
props.put("b", new IntegerProperty());
props.put("a", new FloatProperty());

HashMap<String, Model> 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<String> modelNames = swagger.getDefinitions().keySet();
Assert.assertEquals(modelNames.toArray(), orderedNames);
for (String modelName : modelNames) {
Set<String> propNames = swagger.getDefinitions().get(modelName).getProperties().keySet();
Assert.assertEquals(propNames.toArray(), orderedNames);
}
}
}