Search in sources :

Example 76 with ModelImpl

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

the class GericModelConverter method resolve.

@Override
public Model resolve(Type type, ModelConverterContext context, Iterator<ModelConverter> next) {
    if (type instanceof Class<?>) {
        Class<?> cls = (Class<?>) type;
        if (GenericModel.class.isAssignableFrom(cls)) {
            ModelImpl impl = new ModelImpl();
            impl.setName(cls.getSimpleName());
            for (Entry<String, Class<?>> entry : GenericModel.getDeclaredProperties().entrySet()) {
                impl.addProperty(entry.getKey(), context.resolveProperty(entry.getValue(), null));
            }
            context.defineModel(impl.getName(), impl);
            return impl;
        }
    }
    return null;
}
Also used : ModelImpl(io.swagger.models.ModelImpl)

Example 77 with ModelImpl

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

the class ParameterSerializationTest method serializeBodyParameter.

@Test(description = "it should serialize a BodyParameter")
public void serializeBodyParameter() {
    final ModelImpl model = new ModelImpl().name("Cat").property("name", new StringProperty());
    final BodyParameter p = new BodyParameter().schema(model);
    final String json = "{" + "   \"in\":\"body\"," + "   \"required\":false," + "   \"schema\":{" + "      \"properties\":{" + "         \"name\":{" + "            \"type\":\"string\"" + "         }" + "      }" + "   }" + "}";
    SerializationMatchers.assertEqualsToJson(p, json);
}
Also used : StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) BodyParameter(io.swagger.models.parameters.BodyParameter) Test(org.testng.annotations.Test)

Example 78 with ModelImpl

use of io.swagger.models.ModelImpl 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 79 with ModelImpl

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

the class SimpleReaderTest method scanResourceWithBodyParameters.

@Test(description = "scan a resource with body parameters")
public void scanResourceWithBodyParameters() {
    Swagger swagger = getSwagger(ResourceWithBodyParams.class);
    BodyParameter param = (BodyParameter) getPostParameters(swagger, "/testShort").get(0);
    assertEquals(param.getDescription(), "a short input");
    ModelImpl schema = (ModelImpl) param.getSchema();
    assertEquals(schema.getType(), "integer");
    assertEquals(schema.getFormat(), "int32");
    assertEquals(swagger.getDefinitions().keySet(), Arrays.asList("Tag"));
    testString(swagger, "/testApiString", "input", "String parameter");
    testString(swagger, "/testString", "body", null);
    testObject(swagger, "/testApiObject", "input", "Object parameter");
    testObject(swagger, "/testObject", "body", null);
    List<Operation> operations = new ArrayList<Operation>();
    for (Path item : swagger.getPaths().values()) {
        Operation op = item.getPost();
        if (op.getOperationId().startsWith("testPrimitive")) {
            operations.add(op);
        }
    }
    assertEquals(operations.size(), 16);
    for (Operation item : operations) {
        assertEquals(item.getParameters().size(), 1);
    }
}
Also used : Path(io.swagger.models.Path) Swagger(io.swagger.models.Swagger) ArrayList(java.util.ArrayList) Operation(io.swagger.models.Operation) NicknamedOperation(io.swagger.resources.NicknamedOperation) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) Test(org.testng.annotations.Test)

Example 80 with ModelImpl

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

the class SimpleReaderTest method scanResourceWithEmptyModel.

@Test(description = "correctly model an empty model per 499")
public void scanResourceWithEmptyModel() {
    Map<String, Model> definitions = getSwagger(ResourceWithEmptyModel.class).getDefinitions();
    assertEquals(definitions.size(), 1);
    ModelImpl empty = (ModelImpl) definitions.get("EmptyModel");
    assertEquals(empty.getType(), "object");
    assertNull(empty.getProperties());
    assertNull(empty.getAdditionalProperties(), null);
}
Also used : ResourceWithEmptyModel(io.swagger.resources.ResourceWithEmptyModel) 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) Test(org.testng.annotations.Test)

Aggregations

ModelImpl (io.swagger.models.ModelImpl)112 Test (org.testng.annotations.Test)46 Model (io.swagger.models.Model)43 Property (io.swagger.models.properties.Property)40 StringProperty (io.swagger.models.properties.StringProperty)32 RefProperty (io.swagger.models.properties.RefProperty)29 BodyParameter (io.swagger.models.parameters.BodyParameter)28 ArrayProperty (io.swagger.models.properties.ArrayProperty)27 RefModel (io.swagger.models.RefModel)24 IntegerProperty (io.swagger.models.properties.IntegerProperty)18 Swagger (io.swagger.models.Swagger)16 JavaType (com.fasterxml.jackson.databind.JavaType)15 Map (java.util.Map)15 MapProperty (io.swagger.models.properties.MapProperty)14 Test (org.junit.Test)14 Parameter (io.swagger.models.parameters.Parameter)12 HashMap (java.util.HashMap)12 ArrayModel (io.swagger.models.ArrayModel)11 ComposedModel (io.swagger.models.ComposedModel)11 ArrayList (java.util.ArrayList)11