Search in sources :

Example 21 with MapProperty

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

the class PropertySerializationTest method serializeIntegerMapProperty.

@Test(description = "it should serialize a integer MapProperty")
public void serializeIntegerMapProperty() throws IOException {
    final MapProperty p = new MapProperty(new IntegerProperty());
    final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}";
    assertEquals(m.writeValueAsString(p), json);
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) MapProperty(io.swagger.models.properties.MapProperty) Test(org.testng.annotations.Test)

Example 22 with MapProperty

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

the class PropertySerializationTest method serializeLongMapProperty.

@Test(description = "it should serialize a long MapProperty")
public void serializeLongMapProperty() throws IOException {
    final MapProperty p = new MapProperty(new LongProperty());
    final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}";
    assertEquals(m.writeValueAsString(p), json);
}
Also used : MapProperty(io.swagger.models.properties.MapProperty) LongProperty(io.swagger.models.properties.LongProperty) Test(org.testng.annotations.Test)

Example 23 with MapProperty

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

the class PropertySerializationTest method serializeStringMapProperty.

@Test(description = "it should serialize a string MapProperty")
public void serializeStringMapProperty() throws IOException {
    final MapProperty p = new MapProperty(new StringProperty());
    final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"string\"}}";
    assertEquals(m.writeValueAsString(p), json);
}
Also used : MapProperty(io.swagger.models.properties.MapProperty) StringProperty(io.swagger.models.properties.StringProperty) Test(org.testng.annotations.Test)

Example 24 with MapProperty

use of io.swagger.models.properties.MapProperty 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 25 with MapProperty

use of io.swagger.models.properties.MapProperty in project incubator-servicecomb-java-chassis by apache.

the class AnnotationUtils method generateResponseProperty.

public static Property generateResponseProperty(Swagger swagger, ResponseConfigBase config) throws Error {
    Class<?> responseClass = config.getResponseClass();
    if (responseClass == null || ReflectionUtils.isVoid(responseClass)) {
        return null;
    }
    if (!ClassUtils.isPrimitiveOrWrapper(responseClass)) {
        Map<String, Model> newDefinitions = ModelConverters.getInstance().readAll(responseClass);
        appendDefinition(swagger, newDefinitions);
    }
    Property property = ModelConverters.getInstance().readAsProperty(responseClass);
    // 根据swagger定义这里是区分大小写的, 虽然不明白为何这样做,不过还是不要改标准了
    switch(config.getResponseContainer()) {
        case "List":
            property = new ArrayProperty(property);
            break;
        case "Set":
            property = new ArrayProperty(property);
            ((ArrayProperty) property).setUniqueItems(true);
            break;
        case "Map":
            property = new MapProperty(property);
            break;
        case "":
            // 不必处理
            break;
        default:
            throw new Error("not support responseContainer " + config.getResponseContainer());
    }
    return property;
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) MapProperty(io.swagger.models.properties.MapProperty) Model(io.swagger.models.Model) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty)

Aggregations

MapProperty (io.swagger.models.properties.MapProperty)32 Property (io.swagger.models.properties.Property)26 ArrayProperty (io.swagger.models.properties.ArrayProperty)22 RefProperty (io.swagger.models.properties.RefProperty)16 Test (org.testng.annotations.Test)15 StringProperty (io.swagger.models.properties.StringProperty)13 Model (io.swagger.models.Model)12 IntegerProperty (io.swagger.models.properties.IntegerProperty)12 ObjectProperty (io.swagger.models.properties.ObjectProperty)8 RefModel (io.swagger.models.RefModel)7 ModelImpl (io.swagger.models.ModelImpl)6 JavaType (com.fasterxml.jackson.databind.JavaType)5 ApiModelProperty (io.swagger.annotations.ApiModelProperty)5 FileProperty (io.swagger.models.properties.FileProperty)5 LongProperty (io.swagger.models.properties.LongProperty)5 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)4 ComposedModel (io.swagger.models.ComposedModel)4 Operation (io.swagger.models.Operation)4 Response (io.swagger.models.Response)4 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)4