use of io.swagger.models.RefModel in project java-chassis by ServiceComb.
the class ArgumentsMapperFactory method processBodyFieldArgMapper.
// 将多个provider参数映射为契约body的field
// value为field在provider参数中的下标
protected void processBodyFieldArgMapper(ArgumentsMapperConfig config, int swaggerIdx) {
Class<?>[] swaggerParameterTypes = config.getSwaggerMethod().getParameterTypes();
Class<?> swaggerParameterType = swaggerParameterTypes[swaggerIdx];
BodyParameter bp = (BodyParameter) config.getSwaggerParameters().get(swaggerIdx);
Model model = bp.getSchema();
if (RefModel.class.isInstance(model)) {
String refName = ((RefModel) model).getSimpleRef();
model = config.getSwagger().getDefinitions().get(refName);
}
Map<Integer, Field> fieldMap = new HashMap<>();
for (String propertyName : model.getProperties().keySet()) {
// 理论上应该只在provider的body类型的参数中查找,不过正常定义契约不会有问题的,先不用处理了
int providerIdx = findInParameters(propertyName, config.getProviderParameters());
if (providerIdx >= 0) {
Field field = ReflectionUtils.findField(swaggerParameterType, propertyName);
field.setAccessible(true);
fieldMap.put(providerIdx, field);
continue;
}
String msg = generateSkipParamInfo("body parameter field", config, propertyName);
LOGGER.warn(msg);
}
ArgumentMapper bodyFieldArg = createBodyFieldArgMapper(config, swaggerIdx, fieldMap);
config.addArgumentMapper(bodyFieldArg);
}
use of io.swagger.models.RefModel in project java-chassis by ServiceComb.
the class TestSwaggerUtils method testConverter.
@Test
public void testConverter() {
SwaggerGenerator generator = new SwaggerGeneratorForTest(context, null);
Swagger swagger = generator.getSwagger();
ParamUtils.addDefinitions(swagger, User.class);
Model model = swagger.getDefinitions().get("User");
model.getVendorExtensions().clear();
JavaType javaType = ConverterMgr.findJavaType(generator, model);
checkJavaType(swagger, javaType);
RefModel refModel = new RefModel();
refModel.setReference("User");
javaType = ConverterMgr.findJavaType(generator, refModel);
checkJavaType(swagger, javaType);
}
use of io.swagger.models.RefModel in project java-chassis by ServiceComb.
the class PendingBodyParameter method toRefModel.
// swagger中的body只能是ref,不能是简单类型
private RefModel toRefModel(String modelType) {
if (RefProperty.class.isInstance(property)) {
return (RefModel) PropertyBuilder.toModel(property);
}
ModelImpl modelImpl = new ModelImpl();
modelImpl.setType("object");
modelImpl.setName(name);
modelImpl.addProperty(name, property);
operationGenerator.getSwagger().addDefinition(modelType, modelImpl);
RefModel refModel = new RefModel();
refModel.setReference("#/definitions/" + modelType);
return refModel;
}
use of io.swagger.models.RefModel in project java-chassis by ServiceComb.
the class PendingBodyParameter method createBodyParameter.
public BodyParameter createBodyParameter(String paramName) {
String modelType = ParamUtils.generateBodyParameterName(operationGenerator.getProviderMethod());
RefModel model = toRefModel(modelType);
BodyParameter bodyParameter = new BodyParameter();
bodyParameter.setName(paramName);
bodyParameter.setSchema(model);
return bodyParameter;
}
use of io.swagger.models.RefModel 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;
}
Aggregations