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 @@ -859,17 +859,8 @@ else if(!SCHEMA_KEYS.contains(key)) {
}
JsonNode exampleNode = node.get("example");
if(exampleNode != null) {
// we support text or object nodes
if(exampleNode.getNodeType().equals(JsonNodeType.OBJECT)) {
ObjectNode on = getObject("example", node, false, location, result);
if(on != null) {
model.setExample(on);
}
} else if (exampleNode.isValueNode()) {
model.setExample(exampleNode.asText());
} else {
model.setExample(exampleNode.toString());
}
Object example = Json.mapper().convertValue(exampleNode, Object.class);
model.setExample(example);
}

if(model != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.parser.util.SwaggerDeserializationResult;
import io.swagger.parser.util.TestUtils;
import io.swagger.util.Json;
import io.swagger.util.Yaml;
import org.testng.Assert;
Expand Down Expand Up @@ -487,15 +488,7 @@ public void testIssue292WithCSVCollectionFormat() {
QueryParameter qp = (QueryParameter) param;
assertEquals(qp.getCollectionFormat(), "csv");
}

@Test
public void testIssue255() {
SwaggerParser parser = new SwaggerParser();

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi, why delete this test instead of making it work with the new approach?.

Copy link

Choose a reason for hiding this comment

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

Could I try to make this test work? Again, anything to move this fix along would be great.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, please and rebase as well. Thanks!

Copy link

Choose a reason for hiding this comment

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

So, grab the pull request, fix the test, rebase against the latest, and resubmit the pull request? You know I'm not the original author of this pull request ...

Copy link
Contributor

Choose a reason for hiding this comment

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

oh ok. So how else can we speed this fix?

Copy link

Choose a reason for hiding this comment

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

I'll try to patch up the pull request and see if I can make the test work. Hopefully sometime later today or tomorrow (unless the original author fixes this first).

Copy link

Choose a reason for hiding this comment

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

After some work/investigation and consulting the original issue 255, I believe this fix is correct and the original was not (and this is also indicated/hinted at in the fix of 255). There are unit tests in this fix that verify that a proper ArrayModel is returned for the array example (not a string as in the prior fixes unit test). I also tested with several downstream pipelines (one that takes swagger -> markdown -> asciidoc -> pdf and a similar one that takes swagger -> markdown -> asciidoc -> static html). In each case, the fixed version works (i.e. the final pdf / html with an array of objects is displayed correctly). So, the test was removed because it is somewhat redundant. Also, it seems like you might want the original issue 255 authors/collaborators to weigh in as I do not know what their downstream usage of the swagger-parser is (so this fix could break their usage - I cannot tell).

Copy link

Choose a reason for hiding this comment

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

So, are we good to go? Will this show up soon in a release?

Copy link
Contributor

@gracekarina gracekarina Mar 14, 2018

Choose a reason for hiding this comment

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

Hi we need this is updated with master branch, I do not have permission to update the @boillodmanuel fork, let me check what we can do. 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

@kidnme merged!

Swagger swagger = parser.read("objectExample.json");
assertEquals(swagger.getDefinitions().get("SamplePayload").getExample(), "[{\"op\":\"replace\",\"path\":\"/s\",\"v\":\"w\"}]");
}


@Test
public void testIssue286() {
SwaggerParser parser = new SwaggerParser();
Expand Down Expand Up @@ -853,6 +846,53 @@ public void testBadFormat() throws Exception {
assertEquals(queryParameter.isUniqueItems(), true);
}

@Test
public void testDefinitionExample() throws Exception {
SwaggerParser parser = new SwaggerParser();
final Swagger swagger = parser.read(TestUtils.getResourceAbsolutePath("/definition_example.yaml"));

ModelImpl model;
ArrayModel arrayModel;

model = (ModelImpl)swagger.getDefinitions().get("NumberType");
assertEquals((Double)model.getExample(), 2.0d, 0d);

model = (ModelImpl)swagger.getDefinitions().get("IntegerType");
assertEquals((int)model.getExample(), 2);

model = (ModelImpl)swagger.getDefinitions().get("StringType");
assertEquals((String)model.getExample(), "2");

model = (ModelImpl)swagger.getDefinitions().get("ObjectType");
assertTrue(model.getExample() instanceof Map);
Map objectExample = (Map) model.getExample();
assertEquals((String)objectExample.get("propertyA"), "valueA");
assertEquals((Integer)objectExample.get("propertyB"), new Integer(123));

arrayModel = (ArrayModel)swagger.getDefinitions().get("ArrayType");
assertTrue(arrayModel.getExample() instanceof List);
List<Map> arrayExample = (List<Map>) arrayModel.getExample();
assertEquals((String)arrayExample.get(0).get("propertyA"), "valueA1");
assertEquals((Integer)arrayExample.get(0).get("propertyB"), new Integer(123));
assertEquals((String)arrayExample.get(1).get("propertyA"), "valueA2");
assertEquals((Integer)arrayExample.get(1).get("propertyB"), new Integer(456));

model = (ModelImpl)swagger.getDefinitions().get("NumberTypeStringExample");
assertEquals((String)model.getExample(), "2.0");

model = (ModelImpl)swagger.getDefinitions().get("IntegerTypeStringExample");
assertEquals((String)model.getExample(), "2");

model = (ModelImpl)swagger.getDefinitions().get("StringTypeStringExample");
assertEquals((String)model.getExample(), "2");

model = (ModelImpl)swagger.getDefinitions().get("ObjectTypeStringExample");
assertEquals((String)model.getExample(), "{\"propertyA\": \"valueA\", \"propertyB\": 123}");

arrayModel = (ArrayModel) swagger.getDefinitions().get("ArrayTypeStringExample");
assertEquals((String)arrayModel.getExample(), "[{\"propertyA\": \"valueA1\", \"propertyB\": 123}, {\"propertyA\": \"valueA2\", \"propertyB\": 456}]");
}

@Test
public void testIssue357() {
SwaggerParser parser = new SwaggerParser();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.swagger.parser.util;

import java.io.File;
import java.net.URISyntaxException;

public class TestUtils {

/**
* @param resourceLocation The resource location relative to classpath root (ie: starts with '/')
*/
public static String getResourceAbsolutePath(String resourceLocation) {
if (!resourceLocation.startsWith("/")) {
throw new RuntimeException("resourceLocation should be relative to classpath root (ie: starts with '/')");
}
try {
// we use toURI
return new File(TestUtils.class.getResource(resourceLocation).toURI()).getAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
57 changes: 57 additions & 0 deletions modules/swagger-parser/src/test/resources/definition_example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
swagger: '2.0'
info:
version: 1.0.0
title: Pets Store
paths: {}
definitions:

NumberType:
type: number
example: 2.0

IntegerType:
type: integer
example: 2

StringType:
type: string
example: "2"

ObjectType:
type: object
example:
propertyA: valueA
propertyB: 123

ArrayType:
type: array
items:
type: object
example:
- propertyA: valueA1
propertyB: 123
- propertyA: valueA2
propertyB: 456

NumberTypeStringExample:
type: number
example: "2.0"

IntegerTypeStringExample:
type: integer
example: "2"

StringTypeStringExample:
type: string
example: "2"

ObjectTypeStringExample:
type: object
example: "{\"propertyA\": \"valueA\", \"propertyB\": 123}"

ArrayTypeStringExample:
type: array
items:
type: object
example: "[{\"propertyA\": \"valueA1\", \"propertyB\": 123}, {\"propertyA\": \"valueA2\", \"propertyB\": 456}]"
42 changes: 0 additions & 42 deletions modules/swagger-parser/src/test/resources/objectExample.json

This file was deleted.