Fix for issue #5460 (#5778)

* Fix issue 5460

* Handling only first and last double quote to support example with double quote in the middle

* Fix proposed by @ePaul

* Add comments to explain de fixStringModel function. Add an enum model called PetStatus that test this scenario. Update sample only for JavaPlayFramework generator
This commit is contained in:
Jean-François Côté 2017-07-24 13:20:36 -04:00 committed by wing328
parent 64658f0bea
commit 79e10c427a
4 changed files with 75 additions and 7 deletions

View File

@ -161,7 +161,7 @@ public class InlineModelResolver {
Map<String, Property> 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);

View File

@ -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'

View File

@ -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;
}
}

View File

@ -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" : {