From 5af31fc97cdab38fd2ef2075087d46bf95078af8 Mon Sep 17 00:00:00 2001 From: alena Date: Fri, 14 Jan 2011 11:16:47 -0800 Subject: [PATCH] Modified API xml doc writer - sort request and response parameters in alphabetical order. --- .../com/cloud/api/doc/ApiXmlDocWriter.java | 45 ++++++++++++++++--- server/src/com/cloud/api/doc/Argument.java | 10 ++++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/server/src/com/cloud/api/doc/ApiXmlDocWriter.java b/server/src/com/cloud/api/doc/ApiXmlDocWriter.java index c96dae3c6dc..a53d015f2c5 100644 --- a/server/src/com/cloud/api/doc/ApiXmlDocWriter.java +++ b/server/src/com/cloud/api/doc/ApiXmlDocWriter.java @@ -264,6 +264,9 @@ public class ApiXmlDocWriter { private static ArrayList setRequestFields(Field[] fields) { ArrayList arguments = new ArrayList(); + ArrayList requiredArguments = new ArrayList(); + ArrayList optionalArguments = new ArrayList(); + Argument id = null; for (Field f : fields) { Parameter parameterAnnotation = f.getAnnotation(Parameter.class); if (parameterAnnotation != null && parameterAnnotation.expose()) { @@ -272,14 +275,37 @@ public class ApiXmlDocWriter { if (!parameterAnnotation.description().isEmpty()) { reqArg.setDescription(parameterAnnotation.description()); } - arguments.add(reqArg); + + if (reqArg.isRequired() == true) { + if (parameterAnnotation.name().equals("id")) { + id = reqArg; + } else { + requiredArguments.add(reqArg); + } + } else { + optionalArguments.add(reqArg); + } } } + + Collections.sort(requiredArguments); + Collections.sort(optionalArguments); + + //sort required and optional arguments here + if (id != null) { + arguments.add(id); + } + arguments.addAll(requiredArguments); + arguments.addAll(optionalArguments); + return arguments; } private static ArrayList setResponseFields(Field[] responseFields) { ArrayList arguments = new ArrayList(); + ArrayList sortedArguments = new ArrayList(); + Argument id = null; + for (Field responseField : responseFields) { SerializedName nameAnnotation = responseField.getAnnotation(SerializedName.class); Param paramAnnotation = responseField.getAnnotation(Param.class); @@ -308,11 +334,20 @@ public class ApiXmlDocWriter { } if (toExpose) { - arguments.add(respArg); - } - - + if (nameAnnotation.value().equals("id")) { + id = respArg; + } else { + sortedArguments.add(respArg); + } + } } + + Collections.sort(sortedArguments); + + if (id != null) { + arguments.add(id); + } + arguments.addAll(sortedArguments); return arguments; } diff --git a/server/src/com/cloud/api/doc/Argument.java b/server/src/com/cloud/api/doc/Argument.java index 2846bb550b4..e11e1899873 100644 --- a/server/src/com/cloud/api/doc/Argument.java +++ b/server/src/com/cloud/api/doc/Argument.java @@ -20,7 +20,7 @@ package com.cloud.api.doc; import java.util.List; -public class Argument{ +public class Argument implements Comparable{ private String name; private String description; private Boolean required; @@ -56,4 +56,12 @@ public class Argument{ public void setArguments(List arguments) { this.arguments = arguments; } + + public int compareTo(Object anotherAgrument) throws ClassCastException { + if (!(anotherAgrument instanceof Argument)) + throw new ClassCastException("An Argument object expected."); + Argument argument = (Argument)anotherAgrument; + return this.getName().compareToIgnoreCase(argument.getName()); + } + }