Search in sources :

Example 21 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class SwaggerReaderTest method testIssue207.

@Test
public void testIssue207() throws Exception {
    String spec = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"paths\": {\n" + "    \"/foo\": {\n" + "      \"get\": {\n" + "        \"parameters\": {},\n" + "        \"responses\": {\n" + "          \"200\": {\n" + "            \"description\": \"successful operation\",\n" + "            \"schema\": {\n" + "              \"type\": \"array\",\n" + "              \"items\": {\n" + "                \"$ref\": \"#/definitions/Pet\"\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"definitions\": {\n" + "    \"Pet\": {\n" + "      \"type\": \"object\",\n" + "      \"properties\": {\n" + "        \"name\": {\n" + "          \"type\": \"string\"\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(spec);
    assertNotNull(result);
    Response response = result.getSwagger().getPath("/foo").getGet().getResponses().get("200");
    assertNotNull(response);
    Property schema = response.getSchema();
    assertTrue(schema instanceof ArrayProperty);
    ArrayProperty ap = (ArrayProperty) schema;
    assertTrue(ap.getItems() instanceof RefProperty);
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 22 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class SwaggerReaderTest method testIssue136.

@Test
public void testIssue136() {
    String spec = "swagger: '2.0'\n" + "info:\n" + "  title: issue 136\n" + "paths:\n" + "  /foo:\n" + "    get:\n" + "      parameters: []\n" + "      responses:\n" + "        200:\n" + "          description: 'the pet'\n" + "          schema:\n" + "            $ref: 'http://petstore.swagger.io/v2/swagger.json#/definitions/Pet'";
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(spec);
    Swagger swagger = result.getSwagger();
    Property property = swagger.getPath("/foo").getGet().getResponses().get("200").getSchema();
    assertNotNull(property);
    assertTrue(property instanceof RefProperty);
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 23 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class PropertyProcessorTest method testProcessRefProperty_ExternalRef.

@Test
public void testProcessRefProperty_ExternalRef() throws Exception {
    expectCreationOfExternalRefProcessor();
    final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
    final RefProperty refProperty = new RefProperty(ref);
    expectCallToExternalRefProcessor(ref, RefFormat.URL, "bar");
    new PropertyProcessor(cache, swagger).processProperty(refProperty);
    new FullVerifications() {

        {
        }
    };
    assertEquals(refProperty.get$ref(), "#/definitions/bar");
}
Also used : FullVerifications(mockit.FullVerifications) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 24 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class PropertyProcessorTest method testProcessArrayProperty_ItemsIsRefProperty.

@Test
public void testProcessArrayProperty_ItemsIsRefProperty() throws Exception {
    expectCreationOfExternalRefProcessor();
    final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
    final RefProperty refProperty = new RefProperty(ref);
    ArrayProperty arrayProperty = new ArrayProperty();
    arrayProperty.setItems(refProperty);
    expectCallToExternalRefProcessor(ref, RefFormat.URL, "bar");
    new PropertyProcessor(cache, swagger).processProperty(arrayProperty);
    new FullVerifications() {

        {
        }
    };
    assertEquals(((RefProperty) arrayProperty.getItems()).get$ref(), "#/definitions/bar");
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) FullVerifications(mockit.FullVerifications) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 25 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class SwaggerCompatConverter method propertyFromTypedObject.

public Property propertyFromTypedObject(ExtendedTypedObject obj) {
    String type = obj.getType() == null ? null : obj.getType().toString();
    String format = obj.getFormat() == null ? null : obj.getFormat().toString();
    Property output = null;
    if ("array".equals(type)) {
        ArrayProperty am = new ArrayProperty();
        Items items = obj.getItems();
        if (items == null) {
            LOGGER.error("Error! Missing array type for property!  Assuming `object` -- please fix your spec");
            items = new Items();
            items.setType("object");
        }
        type = items.getType() == null ? null : items.getType().toString();
        format = items.getFormat() == null ? null : items.getFormat().toString();
        Map<PropertyBuilder.PropertyId, Object> args = new HashMap<>();
        if (items.getExtraFields().get("enum") != null && items.getExtraFields().get("enum").isArray()) {
            ArrayNode an = (ArrayNode) items.getExtraFields().get("enum");
            List<String> enumValues = new ArrayList<>();
            for (JsonNode jn : an) {
                enumValues.add(jn.textValue());
            }
            args.put(PropertyBuilder.PropertyId.ENUM, enumValues);
        }
        Property innerType = PropertyBuilder.build(type, format, args);
        if (innerType != null && !(innerType instanceof UntypedProperty)) {
            am.setItems(innerType);
        } else if (items.getRef() != null) {
            am.setItems(new RefProperty(items.getRef()));
        } else {
            am.setItems(new RefProperty(type));
        }
        output = am;
    } else {
        Map<PropertyBuilder.PropertyId, Object> args = new HashMap<PropertyBuilder.PropertyId, Object>();
        if (obj.getEnumValues() != null && obj.getEnumValues().size() > 0) {
            args.put(PropertyBuilder.PropertyId.ENUM, obj.getEnumValues());
        }
        if (obj.getMinimum() != null) {
            args.put(PropertyBuilder.PropertyId.MINIMUM, new BigDecimal(obj.getMinimum()));
        }
        if (obj.getMaximum() != null) {
            args.put(PropertyBuilder.PropertyId.MAXIMUM, new BigDecimal(obj.getMaximum()));
        }
        Property i = PropertyBuilder.build(type, format, args);
        if (i != null && !(i instanceof UntypedProperty)) {
            output = i;
        } else {
            if (obj.getRef() != null) {
                output = new RefProperty(obj.getRef());
            } else if (type != null && !type.equals("void")) {
                output = new RefProperty(type);
            }
        }
    }
    return output;
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) UntypedProperty(io.swagger.models.properties.UntypedProperty) BigDecimal(java.math.BigDecimal) RefProperty(io.swagger.models.properties.RefProperty) Items(io.swagger.models.apideclaration.Items) ExtendedTypedObject(io.swagger.models.apideclaration.ExtendedTypedObject) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) ModelProperty(io.swagger.models.apideclaration.ModelProperty) RefProperty(io.swagger.models.properties.RefProperty) UntypedProperty(io.swagger.models.properties.UntypedProperty) PropertyBuilder(io.swagger.models.properties.PropertyBuilder)

Aggregations

RefProperty (io.swagger.models.properties.RefProperty)74 Property (io.swagger.models.properties.Property)50 ArrayProperty (io.swagger.models.properties.ArrayProperty)46 StringProperty (io.swagger.models.properties.StringProperty)35 Test (org.testng.annotations.Test)35 Model (io.swagger.models.Model)23 MapProperty (io.swagger.models.properties.MapProperty)20 ModelImpl (io.swagger.models.ModelImpl)18 Response (io.swagger.models.Response)18 IntegerProperty (io.swagger.models.properties.IntegerProperty)18 Operation (io.swagger.models.Operation)17 RefModel (io.swagger.models.RefModel)17 LongProperty (io.swagger.models.properties.LongProperty)14 Path (io.swagger.models.Path)12 Swagger (io.swagger.models.Swagger)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 ArrayModel (io.swagger.models.ArrayModel)11 BodyParameter (io.swagger.models.parameters.BodyParameter)11 Map (java.util.Map)11