diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/InlineModelResolver.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/InlineModelResolver.java index 34b0906fd10..283c4c3099a 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/InlineModelResolver.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/InlineModelResolver.java @@ -161,7 +161,7 @@ public class InlineModelResolver { Map properties = m.getProperties(); flattenProperties(properties, modelName); - + fixStringModel(m); } else if (model instanceof ArrayModel) { ArrayModel m = (ArrayModel) model; Property inner = m.getItems(); @@ -191,6 +191,21 @@ public class InlineModelResolver { } } + /** + * This function fix models that are string (mostly enum). Before this fix, the example + * would look something like that in the doc: "\"example from def\"" + * @param m Model implementation + */ + private void fixStringModel(ModelImpl m) { + if (m.getType() != null && m.getType().equals("string") && m.getExample() != null) { + String example = m.getExample().toString(); + if (example.substring(0, 1).equals("\"") && + example.substring(example.length() - 1).equals("\"")) { + m.setExample(example.substring(1, example.length() - 1)); + } + } + } + private String resolveModelName(String title, String key) { if (title == null) { return uniqueName(key); diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml b/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml index e3ba184ea57..4500a44871f 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml @@ -701,6 +701,15 @@ definitions: type: string message: type: string + PetStatus: + type: string + enum: + - Healthy + - Sick + - Quarantined + - InPetsHeaven + description: "Pet's status" + example: Healthy externalDocs: description: Find out more about Swagger url: 'http://swagger.io' diff --git a/samples/server/petstore/java-play-framework/app/apimodels/PetStatus.java b/samples/server/petstore/java-play-framework/app/apimodels/PetStatus.java new file mode 100644 index 00000000000..057b336de32 --- /dev/null +++ b/samples/server/petstore/java-play-framework/app/apimodels/PetStatus.java @@ -0,0 +1,43 @@ +package apimodels; + +import java.util.Objects; +import javax.validation.constraints.*; +import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.annotation.JsonCreator; + +/** + * Pet's status + */ +public enum PetStatus { + + HEALTHY("Healthy"), + + SICK("Sick"), + + QUARANTINED("Quarantined"), + + INPETSHEAVEN("InPetsHeaven"); + + private String value; + + PetStatus(String value) { + this.value = value; + } + + @Override + @JsonValue + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static PetStatus fromValue(String text) { + for (PetStatus b : PetStatus.values()) { + if (String.valueOf(b.value).equals(text)) { + return b; + } + } + return null; + } +} + diff --git a/samples/server/petstore/java-play-framework/public/swagger.json b/samples/server/petstore/java-play-framework/public/swagger.json index c2d08f8e01f..302a6eb43e8 100644 --- a/samples/server/petstore/java-play-framework/public/swagger.json +++ b/samples/server/petstore/java-play-framework/public/swagger.json @@ -900,12 +900,13 @@ } }, "title" : "An uploaded response", - "description" : "Describes the result of uploading an image resource", - "example" : { - "code" : 0, - "type" : "aeiou", - "message" : "aeiou" - } + "description" : "Describes the result of uploading an image resource" + }, + "PetStatus" : { + "type" : "string", + "description" : "Pet's status", + "example" : "Healthy", + "enum" : [ "Healthy", "Sick", "Quarantined", "InPetsHeaven" ] } }, "externalDocs" : {