Search in sources :

Example 6 with ComposedModel

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

the class ModelConverterContextImpl method resolve.

@Override
public Model resolve(Type type) {
    if (processedTypes.contains(type)) {
        return modelByType.get(type);
    } else {
        processedTypes.add(type);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("resolve %s", type));
    }
    Iterator<ModelConverter> converters = this.getConverters();
    Model resolved = null;
    if (converters.hasNext()) {
        ModelConverter converter = converters.next();
        LOGGER.debug("trying extension " + converter);
        resolved = converter.resolve(type, this, converters);
    }
    if (resolved != null) {
        modelByType.put(type, resolved);
        Model resolvedImpl = resolved;
        if (resolvedImpl instanceof ComposedModel) {
            resolvedImpl = ((ComposedModel) resolved).getChild();
        }
        if (resolvedImpl instanceof ModelImpl) {
            ModelImpl impl = (ModelImpl) resolvedImpl;
            if (impl.getName() != null) {
                modelByName.put(impl.getName(), resolved);
            }
        }
    }
    return resolved;
}
Also used : ComposedModel(io.swagger.models.ComposedModel) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) ModelImpl(io.swagger.models.ModelImpl)

Example 7 with ComposedModel

use of io.swagger.models.ComposedModel 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 8 with ComposedModel

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

the class InheritedBeanTest method testMultipleInheritedBean.

@Test
public void testMultipleInheritedBean() throws Exception {
    final Model baseModel = context.resolve(MultipleBaseBean.class);
    assertNotNull(baseModel);
    assertBasePropertiesValid(baseModel.getProperties());
    final Model sub1Model = context.getDefinedModels().get("MultipleSub1Bean");
    assertNotNull(sub1Model);
    // make sure child points at parent
    assertTrue(sub1Model instanceof ComposedModel);
    ComposedModel cm1 = (ComposedModel) sub1Model;
    assertEquals(cm1.getParent().getReference(), "#/definitions/MultipleBaseBean");
    // make sure parent properties are filtered out of subclass
    assertSub1PropertiesValid(cm1.getChild().getProperties());
    final Model sub2Model = context.getDefinedModels().get("MultipleSub2Bean");
    assertNotNull(sub2Model);
    // make sure child points at parent
    assertTrue(sub2Model instanceof ComposedModel);
    ComposedModel cm2 = (ComposedModel) sub2Model;
    assertEquals(cm2.getParent().getReference(), "#/definitions/MultipleBaseBean");
    // make sure parent properties are filtered out of subclass
    assertSub2PropertiesValid(cm2.getChild().getProperties());
}
Also used : ComposedModel(io.swagger.models.ComposedModel) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) ApiModel(io.swagger.annotations.ApiModel) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 9 with ComposedModel

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

the class InheritedBeanTest method testInheritedBean.

@Test
public void testInheritedBean() throws Exception {
    final Model baseModel = context.resolve(BaseBean.class);
    assertNotNull(baseModel);
    assertBasePropertiesValid(baseModel.getProperties());
    final Model subModel = context.getDefinedModels().get("Sub1Bean");
    assertNotNull(subModel);
    // make sure child points at parent
    assertTrue(subModel instanceof ComposedModel);
    ComposedModel cm = (ComposedModel) subModel;
    assertEquals(cm.getParent().getReference(), "#/definitions/BaseBean");
    // make sure parent properties are filtered out of subclass
    assertSub1PropertiesValid(cm.getChild().getProperties());
}
Also used : ComposedModel(io.swagger.models.ComposedModel) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) ApiModel(io.swagger.annotations.ApiModel) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 10 with ComposedModel

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

the class InheritedBeanTest method testMultipleInheritedChildBean.

@Test
public void testMultipleInheritedChildBean() throws Exception {
    final Model subModel = context.resolve(MultipleSub1Bean.class);
    assertNotNull(subModel);
    // make sure child points at parent
    assertTrue(subModel instanceof ComposedModel);
    ComposedModel cm = (ComposedModel) subModel;
    assertEquals(cm.getParent().getReference(), "#/definitions/MultipleBaseBean");
    // make sure parent properties are filtered out of subclass
    assertSub1PropertiesValid(cm.getChild().getProperties());
    final Model baseModel = context.getDefinedModels().get("MultipleBaseBean");
    assertNotNull(baseModel);
    assertBasePropertiesValid(baseModel.getProperties());
    final Model sub1Model = context.getDefinedModels().get("MultipleSub1Bean");
    assertNotNull(sub1Model);
    // make sure child points at parent
    assertTrue(sub1Model instanceof ComposedModel);
    ComposedModel cm1 = (ComposedModel) sub1Model;
    assertEquals(cm1.getParent().getReference(), "#/definitions/MultipleBaseBean");
    // make sure parent properties are filtered out of subclass
    assertSub1PropertiesValid(cm1.getChild().getProperties());
    final Model sub2Model = context.getDefinedModels().get("MultipleSub2Bean");
    assertNotNull(sub2Model);
    // make sure child points at parent
    assertTrue(sub2Model instanceof ComposedModel);
    ComposedModel cm2 = (ComposedModel) sub2Model;
    assertEquals(cm2.getParent().getReference(), "#/definitions/MultipleBaseBean");
    // make sure parent properties are filtered out of subclass
    assertSub2PropertiesValid(cm2.getChild().getProperties());
}
Also used : ComposedModel(io.swagger.models.ComposedModel) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) ApiModel(io.swagger.annotations.ApiModel) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

ComposedModel (io.swagger.models.ComposedModel)11 Model (io.swagger.models.Model)11 ApiModel (io.swagger.annotations.ApiModel)8 ModelImpl (io.swagger.models.ModelImpl)7 RefModel (io.swagger.models.RefModel)6 ArrayProperty (io.swagger.models.properties.ArrayProperty)5 MapProperty (io.swagger.models.properties.MapProperty)5 RefProperty (io.swagger.models.properties.RefProperty)5 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)4 ApiModelProperty (io.swagger.annotations.ApiModelProperty)4 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)4 IntegerProperty (io.swagger.models.properties.IntegerProperty)4 Property (io.swagger.models.properties.Property)4 StringProperty (io.swagger.models.properties.StringProperty)4 UUIDProperty (io.swagger.models.properties.UUIDProperty)4 BeforeTest (org.testng.annotations.BeforeTest)4 Test (org.testng.annotations.Test)4 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)3 LinkedHashMap (java.util.LinkedHashMap)3 JavaType (com.fasterxml.jackson.databind.JavaType)2