Search in sources :

Example 66 with ModelImpl

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

the class ModelResolver method resolveSubtypes.

private boolean resolveSubtypes(ModelImpl model, BeanDescription bean, ModelConverterContext context) {
    final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
    if (types == null) {
        return false;
    }
    /**
         * As the introspector will find @JsonSubTypes for a child class that are present on its super classes, the
         * code segment below will also run the introspector on the parent class, and then remove any sub-types that are
         * found for the parent from the sub-types found for the child. The same logic all applies to implemented
         * interfaces, and is accounted for below.
         */
    removeSuperClassAndInterfaceSubTypes(types, bean);
    int count = 0;
    final Class<?> beanClass = bean.getClassInfo().getAnnotated();
    for (NamedType subtype : types) {
        final Class<?> subtypeType = subtype.getType();
        if (!beanClass.isAssignableFrom(subtypeType)) {
            continue;
        }
        final Model subtypeModel = context.resolve(subtypeType);
        if (subtypeModel instanceof ModelImpl) {
            final ModelImpl impl = (ModelImpl) subtypeModel;
            // check if model name was inherited
            if (impl.getName().equals(model.getName())) {
                impl.setName(_typeNameResolver.nameForType(_mapper.constructType(subtypeType), TypeNameResolver.Options.SKIP_API_MODEL));
            }
            // remove shared properties defined in the parent
            final Map<String, Property> baseProps = model.getProperties();
            final Map<String, Property> subtypeProps = impl.getProperties();
            if (baseProps != null && subtypeProps != null) {
                for (Map.Entry<String, Property> entry : baseProps.entrySet()) {
                    if (entry.getValue().equals(subtypeProps.get(entry.getKey()))) {
                        subtypeProps.remove(entry.getKey());
                    }
                }
            }
            impl.setDiscriminator(null);
            ComposedModel child = new ComposedModel().parent(new RefModel(model.getName())).child(impl);
            context.defineModel(impl.getName(), child, subtypeType, null);
            ++count;
        }
    }
    return count != 0;
}
Also used : RefModel(io.swagger.models.RefModel) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ApiModel(io.swagger.annotations.ApiModel) ModelImpl(io.swagger.models.ModelImpl) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) UUIDProperty(io.swagger.models.properties.UUIDProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 67 with ModelImpl

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

the class ParameterProcessorTest method implicitParameterProcessorTest.

@Test(description = "parse implicit parameters from method")
public void implicitParameterProcessorTest() throws NoSuchMethodException {
    final ApiImplicitParams params = getClass().getDeclaredMethod("implicitParametrizedMethod").getAnnotation(ApiImplicitParams.class);
    final PathParameter param0 = (PathParameter) ParameterProcessor.applyAnnotations(null, new PathParameter(), String.class, Collections.<Annotation>singletonList(params.value()[0]));
    assertNotNull(param0);
    assertEquals(param0.getIn(), "path");
    assertEquals(param0.getName(), "paramName1");
    assertEquals(param0.getDescription(), "paramValue1");
    assertNull(param0.getEnum());
    assertNotNull(param0.getItems());
    final BodyParameter param1 = (BodyParameter) ParameterProcessor.applyAnnotations(null, new BodyParameter(), String.class, Collections.<Annotation>singletonList(params.value()[1]));
    assertNotNull(param1);
    assertEquals(param1.getIn(), "body");
    assertEquals(param1.getName(), "body");
    assertEquals(param1.getDescription(), "paramValue2");
    assertEquals(param1.getAccess(), "test");
    final ModelImpl model = (ModelImpl) param1.getSchema();
    assertNotNull(model);
    assertEquals(model.getDefaultValue(), "10");
}
Also used : ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) PathParameter(io.swagger.models.parameters.PathParameter) Annotation(java.lang.annotation.Annotation) Test(org.testng.annotations.Test)

Example 68 with ModelImpl

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

the class ModelConverterTest method setReadOnly.

@Test(description = "it should set readOnly per #854")
public void setReadOnly() {
    final Map<String, Model> schemas = readAll(JacksonReadonlyModel.class);
    final ModelImpl model = (ModelImpl) schemas.get("JacksonReadonlyModel");
    final Property prop = model.getProperties().get("count");
    assertTrue(prop.getReadOnly());
}
Also used : AbstractModelWithApiModel(io.swagger.models.composition.AbstractModelWithApiModel) Model(io.swagger.models.Model) ModelWithApiModel(io.swagger.models.ModelWithApiModel) EmptyModel(io.swagger.models.EmptyModel) JodaDateTimeModel(io.swagger.models.JodaDateTimeModel) JacksonReadonlyModel(io.swagger.models.JacksonReadonlyModel) ModelImpl(io.swagger.models.ModelImpl) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) DecimalProperty(io.swagger.models.properties.DecimalProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) MapProperty(io.swagger.models.properties.MapProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) BaseIntegerProperty(io.swagger.models.properties.BaseIntegerProperty) ModelWithUrlProperty(io.swagger.models.composition.ModelWithUrlProperty) RefProperty(io.swagger.models.properties.RefProperty) FloatProperty(io.swagger.models.properties.FloatProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Test(org.testng.annotations.Test)

Example 69 with ModelImpl

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

the class ModelConverterTest method processModelWithPairProperties.

@Test(description = "it should process a model with org.apache.commons.lang3.tuple.Pair properties")
public void processModelWithPairProperties() {
    final ModelWithTuple2.TupleAsMapModelConverter asMapConverter = new ModelWithTuple2.TupleAsMapModelConverter(Json.mapper());
    ModelConverters.getInstance().addConverter(asMapConverter);
    final Map<String, Model> asMap = readAll(ModelWithTuple2.class);
    ModelConverters.getInstance().removeConverter(asMapConverter);
    assertEquals(asMap.size(), 4);
    for (String item : Arrays.asList("MapOfString", "MapOfComplexLeft")) {
        ModelImpl model = (ModelImpl) asMap.get(item);
        assertEquals(model.getType(), "object");
        assertNull(model.getProperties());
        assertNotNull(model.getAdditionalProperties());
    }
    final ModelWithTuple2.TupleAsMapPropertyConverter asPropertyConverter = new ModelWithTuple2.TupleAsMapPropertyConverter(Json.mapper());
    ModelConverters.getInstance().addConverter(asPropertyConverter);
    final Map<String, Model> asProperty = readAll(ModelWithTuple2.class);
    ModelConverters.getInstance().removeConverter(asPropertyConverter);
    assertEquals(asProperty.size(), 2);
    for (Map.Entry<String, Property> entry : asProperty.get("ModelWithTuple2").getProperties().entrySet()) {
        String name = entry.getKey();
        Property property = entry.getValue();
        if ("timesheetStates".equals(name)) {
            assertEquals(property.getClass(), MapProperty.class);
        } else if ("manyPairs".equals(name)) {
            assertEquals(property.getClass(), ArrayProperty.class);
            Property items = ((ArrayProperty) property).getItems();
            assertNotNull(items);
            assertEquals(items.getClass(), MapProperty.class);
            Property stringProperty = ((MapProperty) items).getAdditionalProperties();
            assertNotNull(stringProperty);
            assertEquals(stringProperty.getClass(), StringProperty.class);
        } else if ("complexLeft".equals(name)) {
            assertEquals(property.getClass(), ArrayProperty.class);
            Property items = ((ArrayProperty) property).getItems();
            assertNotNull(items);
            assertEquals(items.getClass(), MapProperty.class);
            Property additionalProperty = ((MapProperty) items).getAdditionalProperties();
            assertNotNull(additionalProperty);
            assertEquals(additionalProperty.getClass(), RefProperty.class);
            assertEquals(((RefProperty) additionalProperty).getSimpleRef(), "ComplexLeft");
        } else {
            fail(String.format("Unexpected property: %s", name));
        }
    }
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) ModelWithTuple2(io.swagger.models.ModelWithTuple2) MapProperty(io.swagger.models.properties.MapProperty) StringProperty(io.swagger.models.properties.StringProperty) AbstractModelWithApiModel(io.swagger.models.composition.AbstractModelWithApiModel) Model(io.swagger.models.Model) ModelWithApiModel(io.swagger.models.ModelWithApiModel) EmptyModel(io.swagger.models.EmptyModel) JodaDateTimeModel(io.swagger.models.JodaDateTimeModel) JacksonReadonlyModel(io.swagger.models.JacksonReadonlyModel) ModelImpl(io.swagger.models.ModelImpl) Map(java.util.Map) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) DecimalProperty(io.swagger.models.properties.DecimalProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) MapProperty(io.swagger.models.properties.MapProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) BaseIntegerProperty(io.swagger.models.properties.BaseIntegerProperty) ModelWithUrlProperty(io.swagger.models.composition.ModelWithUrlProperty) RefProperty(io.swagger.models.properties.RefProperty) FloatProperty(io.swagger.models.properties.FloatProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Test(org.testng.annotations.Test)

Example 70 with ModelImpl

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

the class ModelPropertyTest method testReadOnlyProperty.

@Test
public void testReadOnlyProperty() {
    final Map<String, Model> models = ModelConverters.getInstance().readAll(ReadOnlyFields.class);
    ModelImpl model = (ModelImpl) models.get("ReadOnlyFields");
    assertTrue(model.getProperties().get("id").getReadOnly());
}
Also used : Model(io.swagger.models.Model) 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