From 2edfb96c36e769e22057bf56f5e42b0dd842077c Mon Sep 17 00:00:00 2001 From: russellb337 Date: Thu, 7 May 2015 12:02:25 -0700 Subject: [PATCH] swagger codegen should honor global mime types --- .../swagger/codegen/DefaultCodegen.java | 10 +++++--- .../swagger/codegen/DefaultGenerator.java | 25 +++++++++++++++++++ .../src/main/resources/JavaJaxRS/api.mustache | 2 ++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java index 6f51374c53..6275421d77 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultCodegen.java @@ -692,11 +692,13 @@ public class DefaultCodegen { for(String key: operation.getConsumes()) { Map mediaType = new HashMap(); mediaType.put("mediaType", key); - if (count < operation.getConsumes().size()) - mediaType.put("hasMore", "true"); - else - mediaType.put("hasMore", null); count += 1; + if (count < operation.getConsumes().size()) { + mediaType.put("hasMore", "true"); + } + else { + mediaType.put("hasMore", null); + } c.add(mediaType); } op.consumes = c; diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultGenerator.java index 1750997321..634a93c30c 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/DefaultGenerator.java @@ -165,6 +165,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { operation.putAll(config.additionalProperties()); operation.put("classname", config.toApiName(tag)); operation.put("classVarName", config.toApiVarName(tag)); + processMimeTypes(swagger.getConsumes(), operation, "consumes"); + processMimeTypes(swagger.getProduces(), operation, "produces"); + allOperations.add(new HashMap(operation)); for (int i = 0; i < allOperations.size(); i++) { @@ -291,6 +294,28 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { return files; } + private void processMimeTypes(List mimeTypeList, Map operation, String source) { + if(mimeTypeList != null && mimeTypeList.size() > 0) { + List> c = new ArrayList>(); + int count = 0; + for(String key: mimeTypeList) { + Map mediaType = new HashMap(); + mediaType.put("mediaType", key); + count += 1; + if (count < mimeTypeList.size()) { + mediaType.put("hasMore", "true"); + } + else { + mediaType.put("hasMore", null); + } + c.add(mediaType); + } + operation.put(source, c); + String flagFieldName = "has" + source.substring(0, 1).toUpperCase() + source.substring(1); + operation.put(flagFieldName, true); + } + } + public Map> processPaths(Map paths) { Map> ops = new HashMap>(); diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache index b5ebb30f19..832a8bb824 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache @@ -21,6 +21,8 @@ import javax.ws.rs.core.Response; import javax.ws.rs.*; @Path("/{{baseName}}") +{{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}} +{{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}} @com.wordnik.swagger.annotations.Api(value = "/{{baseName}}", description = "the {{baseName}} API") {{#operations}} public class {{classname}} {