Search in sources :

Example 6 with RefModel

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);
}
Also used : RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) BodyParameter(io.swagger.models.parameters.BodyParameter) Field(java.lang.reflect.Field) RefModel(io.swagger.models.RefModel) Model(io.swagger.models.Model)

Example 7 with RefModel

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);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) RefModel(io.swagger.models.RefModel) Swagger(io.swagger.models.Swagger) RefModel(io.swagger.models.RefModel) Model(io.swagger.models.Model) SwaggerGeneratorForTest(io.servicecomb.swagger.generator.core.unittest.SwaggerGeneratorForTest) Test(org.junit.Test) SwaggerGeneratorForTest(io.servicecomb.swagger.generator.core.unittest.SwaggerGeneratorForTest)

Example 8 with RefModel

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;
}
Also used : RefModel(io.swagger.models.RefModel) ModelImpl(io.swagger.models.ModelImpl)

Example 9 with 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;
}
Also used : RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 10 with RefModel

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;
}
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

RefModel (io.swagger.models.RefModel)13 Model (io.swagger.models.Model)7 BodyParameter (io.swagger.models.parameters.BodyParameter)7 Test (org.testng.annotations.Test)6 ModelImpl (io.swagger.models.ModelImpl)4 Operation (io.swagger.models.Operation)4 RefProperty (io.swagger.models.properties.RefProperty)4 StringProperty (io.swagger.models.properties.StringProperty)4 ArrayModel (io.swagger.models.ArrayModel)3 Path (io.swagger.models.Path)3 Response (io.swagger.models.Response)3 Swagger (io.swagger.models.Swagger)3 ArrayProperty (io.swagger.models.properties.ArrayProperty)3 IntegerProperty (io.swagger.models.properties.IntegerProperty)3 Property (io.swagger.models.properties.Property)3 HashMap (java.util.HashMap)3 ComposedModel (io.swagger.models.ComposedModel)2 PathParameter (io.swagger.models.parameters.PathParameter)2 QueryParameter (io.swagger.models.parameters.QueryParameter)2 LongProperty (io.swagger.models.properties.LongProperty)2