Skip to content

Commit

Permalink
Merge pull request #2791 from swagger-api/ticket-2790
Browse files Browse the repository at this point in the history
refs #2790 - fix missing enum in converted response schema
  • Loading branch information
frantuma authored May 3, 2018
2 parents 6f5c1d9 + 84dcac4 commit 81df520
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,31 @@ public void convertToStringProperty()throws Exception{
Assert.assertEquals(property.getFormat(),"password");
}

@Test
public void convertToStringWithEnumProperty()throws Exception{
String yaml = " produces:\n" +
" - application/json\n" +
" parameters:\n" +
" []\n" +
" responses:\n" +
" 200:\n" +
" description: OK\n" +
" schema:\n" +
" type: string\n"+
" enum:\n" +
" - a\n" +
" - b\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Assert.assertTrue(property instanceof StringProperty);
Assert.assertEquals(property.getType(),"string");
Assert.assertEquals(((StringProperty)property).getEnum().size(),2);
}

@Test
public void convertToStringNewProperty()throws Exception{
String yaml = " produces:\n" +
Expand Down Expand Up @@ -583,6 +608,32 @@ public void convertStringProperty()throws Exception{
Assert.assertEquals(model.getExample(),"Hello");
}

@Test
public void convertStringPropertyWithEnum()throws Exception{
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
final JsonNode rootNode = mapper.readTree(Files.readAllBytes(java.nio.file.Paths.get(getClass().getResource("/specFiles/responses.yaml").toURI())));

String specAsYaml = rootNode.toString();

Swagger swagger = Yaml.mapper().readValue(specAsYaml, Swagger.class);

Path string = swagger.getPaths().get("/stringenum");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Property property = response.getSchema();

Assert.assertEquals(((StringProperty)property).getEnum().size(),2);

PropertyModelConverter converter = new PropertyModelConverter();
Model convertedModel = converter.propertyToModel(property);

Assert.assertTrue(convertedModel instanceof ModelImpl);
ModelImpl model = (ModelImpl) convertedModel;
Assert.assertEquals(model.getType(),"string");
Assert.assertEquals(model.getExample(),"Hello");
Assert.assertEquals(model.getEnum().size(),2);
}

@Test
public void convertStringRefProperty()throws Exception{
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Expand Down
11 changes: 11 additions & 0 deletions modules/swagger-core/src/test/resources/specFiles/responses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ paths:
description: OK
schema:
$ref: '#/definitions/DayOfWeekAsString'
/stringenum:
get:
responses:
200:
description: OK
schema:
type: string
enum:
- Hello
- Goodbye
example: Hello
/integer:
get:
responses:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public Model propertyToModel(Property property){
model.setPattern(stringProperty.getPattern());
model.setMinLength(stringProperty.getMinLength());
model.setMaxLength(stringProperty.getMaxLength());
model.setEnum(stringProperty.getEnum());
}

if(property instanceof AbstractNumericProperty) {
Expand Down

0 comments on commit 81df520

Please # to comment.