Search in sources :

Example 41 with RefProperty

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

the class ModelSerializerTest method serializeArrayModel.

@Test(description = "it should serialize an array model")
public void serializeArrayModel() throws IOException {
    final ArrayModel model = new ArrayModel();
    model.setItems(new RefProperty("Pet"));
    assertEquals(m.writeValueAsString(model), "{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/Pet\"}}");
}
Also used : ArrayModel(io.swagger.models.ArrayModel) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 42 with RefProperty

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

the class Reader method addResponse.

private void addResponse(Operation operation, ApiResponse apiResponse) {
    Map<String, Property> responseHeaders = parseResponseHeaders(apiResponse.responseHeaders());
    Response response = new Response().description(apiResponse.message()).headers(responseHeaders);
    if (apiResponse.code() == 0) {
        operation.defaultResponse(response);
    } else {
        operation.response(apiResponse.code(), response);
    }
    if (StringUtils.isNotEmpty(apiResponse.reference())) {
        response.schema(new RefProperty(apiResponse.reference()));
    } else if (!isVoid(apiResponse.response())) {
        Type responseType = apiResponse.response();
        final Property property = ModelConverters.getInstance().readAsProperty(responseType);
        if (property != null) {
            response.schema(ContainerWrapper.wrapContainer(apiResponse.responseContainer(), property));
            appendModels(responseType);
        }
    }
}
Also used : Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) ParameterizedType(java.lang.reflect.ParameterizedType) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty) RefProperty(io.swagger.models.properties.RefProperty)

Example 43 with RefProperty

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

the class PropertySerializationTest method serializeRefProperty.

@Test(description = "it should serialize a RefProperty")
public void serializeRefProperty() throws IOException {
    final RefProperty p = new RefProperty("Dog");
    final String json = "{\"$ref\":\"#/definitions/Dog\"}";
    assertEquals(m.writeValueAsString(p), json);
}
Also used : RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 44 with RefProperty

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

the class SimpleReaderTest method checkResponseModelsProcessing.

@Test(description = "check response models processing")
public void checkResponseModelsProcessing() {
    Swagger swagger = getSwagger(ResourceWithTypedResponses.class);
    assertEquals(swagger.getDefinitions().keySet(), Arrays.asList("Tag"));
    for (Map.Entry<String, Path> entry : swagger.getPaths().entrySet()) {
        String name = entry.getKey().substring(entry.getKey().lastIndexOf("/") + 1);
        if ("testPrimitiveResponses".equals(name)) {
            Map<String, String[]> expected = ImmutableMap.of("400", new String[] { "string", "uri" }, "401", new String[] { "string", "url" }, "402", new String[] { "string", "uuid" }, "403", new String[] { "integer", "int64" }, "404", new String[] { "string", null });
            assertEquals(entry.getValue().getGet().getResponses().size(), expected.size());
            for (Map.Entry<String, Response> responseEntry : entry.getValue().getGet().getResponses().entrySet()) {
                String[] expectedProp = expected.get(responseEntry.getKey());
                Property property = responseEntry.getValue().getSchema();
                assertEquals(property.getType(), expectedProp[0]);
                assertEquals(property.getFormat(), expectedProp[1]);
            }
        } else {
            Operation op = entry.getValue().getGet();
            Property response = op.getResponses().get("200").getSchema();
            Model model = ((BodyParameter) op.getParameters().get(0)).getSchema();
            assertEquals(op.getParameters().size(), 1);
            if ("testObjectResponse".equals(name)) {
                assertEquals(((RefProperty) response).getSimpleRef(), "Tag");
                assertEquals(((RefModel) model).getSimpleRef(), "Tag");
            } else if ("testObjectsResponse".equals(name)) {
                assertEquals(((RefProperty) ((ArrayProperty) response).getItems()).getSimpleRef(), "Tag");
                assertEquals(((RefProperty) ((ArrayModel) model).getItems()).getSimpleRef(), "Tag");
            } else if ("testStringResponse".equals(name)) {
                assertEquals(response.getClass(), StringProperty.class);
                assertEquals(((ModelImpl) model).getType(), "string");
            } else if ("testStringsResponse".equals(name)) {
                assertEquals(((ArrayProperty) response).getItems().getClass(), StringProperty.class);
                assertEquals(((ArrayModel) model).getItems().getClass(), StringProperty.class);
            } else if ("testMapResponse".equals(name)) {
                assertEquals(((RefProperty) ((MapProperty) response).getAdditionalProperties()).getSimpleRef(), "Tag");
                assertNull(model.getProperties());
                assertEquals(((RefProperty) ((ModelImpl) model).getAdditionalProperties()).getSimpleRef(), "Tag");
            } else {
                fail(String.format("Unexpected property: %s", name));
            }
        }
    }
}
Also used : Path(io.swagger.models.Path) MapProperty(io.swagger.models.properties.MapProperty) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) NicknamedOperation(io.swagger.resources.NicknamedOperation) BodyParameter(io.swagger.models.parameters.BodyParameter) RefProperty(io.swagger.models.properties.RefProperty) ResourceWithResponse(io.swagger.resources.ResourceWithResponse) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) ResourceWithEmptyModel(io.swagger.resources.ResourceWithEmptyModel) RefModel(io.swagger.models.RefModel) ArrayModel(io.swagger.models.ArrayModel) ModelImpl(io.swagger.models.ModelImpl) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Test(org.testng.annotations.Test)

Example 45 with RefProperty

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

the class ReferenceTest method scanModel.

@Test(description = "Scan a model with common reference and reference with ApiModel")
public void scanModel() {
    final Map<String, Property> props = ModelConverters.getInstance().readAll(Pet.class).get("Pet").getProperties();
    final RefProperty category = (RefProperty) props.get("category");
    assertEquals(category.getType(), "ref");
    assertEquals(category.get$ref(), "#/definitions/Category");
    final RefProperty categoryWithApiModel = (RefProperty) props.get("categoryWithApiModel");
    assertEquals(categoryWithApiModel.getType(), "ref");
    assertEquals(categoryWithApiModel.get$ref(), "#/definitions/MyCategory");
}
Also used : RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

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