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;
}
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;
}
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());
}
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());
}
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());
}
Aggregations