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