Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void flatten(Swagger swagger) {

Map<String, Property> properties = m.getProperties();
flattenProperties(properties, modelName);

fixStringModel(m);
} else if (model instanceof ArrayModel) {
ArrayModel m = (ArrayModel) model;
Property inner = m.getItems();
Expand Down Expand Up @@ -191,6 +191,21 @@ public void flatten(Swagger swagger) {
}
}

/**
* 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);
Expand Down
9 changes: 9 additions & 0 deletions modules/swagger-codegen/src/test/resources/2_0/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,15 @@ definitions:
type: string
message:
type: string
PetStatus:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JFCote I'll remove this tomorrow add the enum example to https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml instead.

When the Play generator, we should consider using petstore-with-fake-endpoints-models-for-testing.yaml, which covers a lot more edge cases.

type: string
enum:
- Healthy
- Sick
- Quarantined
- InPetsHeaven
description: "Pet's status"
example: Healthy
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
Original file line number Diff line number Diff line change
@@ -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;
}
}

13 changes: 7 additions & 6 deletions samples/server/petstore/java-play-framework/public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : {
Expand Down