Search in sources :

Example 11 with ComposedModel

use of io.swagger.models.ComposedModel in project candlepin by candlepin.

the class CandlepinSwaggerModelConverter method resolveSubtypes.

private boolean resolveSubtypes(ModelImpl model, BeanDescription bean, ModelConverterContext context) {
    final List<NamedType> types = pIntr.findSubtypes(bean.getClassInfo());
    if (types == null) {
        return false;
    }
    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(pTypeNameResolver.nameForType(pMapper.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);
            ++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)

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