Search in sources :

Example 1 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project java-chassis by ServiceComb.

the class ClassUtils method getOrCreateBodyClass.

// 将一系列body parameter包装成一个class
public static Class<?> getOrCreateBodyClass(OperationGenerator operationGenerator, List<BodyParameter> bodyParameters) {
    SwaggerGenerator swaggerGenerator = operationGenerator.getSwaggerGenerator();
    Method method = operationGenerator.getProviderMethod();
    String clsName = swaggerGenerator.ensureGetPackageName() + "." + method.getName() + "Body";
    Class<?> cls = getClassByName(swaggerGenerator.getClassLoader(), clsName);
    if (cls != null) {
        return cls;
    }
    ClassConfig classConfig = new ClassConfig();
    classConfig.setClassName(clsName);
    // 2.预备body与明确body混合
    for (BodyParameter bp : bodyParameters) {
        JavaType javaType = ConverterMgr.findJavaType(swaggerGenerator.getClassLoader(), swaggerGenerator.ensureGetPackageName(), swaggerGenerator.getSwagger(), bp);
        classConfig.addField(bp.getName(), javaType);
    }
    return JavassistUtils.createClass(classConfig);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SwaggerGenerator(io.servicecomb.swagger.generator.core.SwaggerGenerator) ClassConfig(io.servicecomb.common.javassist.ClassConfig) Method(java.lang.reflect.Method) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 2 with BodyParameter

use of io.swagger.models.parameters.BodyParameter 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 3 with BodyParameter

use of io.swagger.models.parameters.BodyParameter 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 4 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project java-chassis by ServiceComb.

the class OperationGenerator method replaceBodyBasedParameter.

protected void replaceBodyBasedParameter(Parameter bodyBasedParameter) {
    if (ParamUtils.isRealBodyParameter(bodyBasedParameter)) {
        return;
    }
    int idx = swaggerParameters.indexOf(bodyBasedParameter);
    String bodyParamName = bodyBasedParameter.getName();
    BodyParameter bodyParameter = ((PendingBodyParameter) bodyBasedParameter).createBodyParameter(bodyParamName);
    swaggerParameters.set(idx, bodyParameter);
}
Also used : PendingBodyParameter(io.servicecomb.swagger.extend.parameter.PendingBodyParameter) PendingBodyParameter(io.servicecomb.swagger.extend.parameter.PendingBodyParameter) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 5 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project swagger-core by swagger-api.

the class SimpleReaderTest method scanClassWithImplicitExamplePostClass.

@Test(description = "scan a resource with operation implicit post example (dataTypeClass)")
public void scanClassWithImplicitExamplePostClass() {
    Swagger swagger = getSwagger(ClassWithExamplePostClass.class);
    Parameter param = swagger.getPaths().get("/external/info2").getPost().getParameters().get(0);
    BodyParameter bp = (BodyParameter) param;
    assertNotNull(bp.getExamples());
    assertTrue(bp.getExamples().size() == 1);
    String value = bp.getExamples().get("application/json");
    assertEquals("[\"a\",\"b\"]", value);
}
Also used : Swagger(io.swagger.models.Swagger) SerializableParameter(io.swagger.models.parameters.SerializableParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Test(org.testng.annotations.Test)

Aggregations

BodyParameter (io.swagger.models.parameters.BodyParameter)96 Test (org.testng.annotations.Test)42 Parameter (io.swagger.models.parameters.Parameter)34 Model (io.swagger.models.Model)30 Operation (io.swagger.models.Operation)28 QueryParameter (io.swagger.models.parameters.QueryParameter)26 ModelImpl (io.swagger.models.ModelImpl)23 Swagger (io.swagger.models.Swagger)23 PathParameter (io.swagger.models.parameters.PathParameter)23 RefModel (io.swagger.models.RefModel)22 ArrayModel (io.swagger.models.ArrayModel)21 Property (io.swagger.models.properties.Property)21 Path (io.swagger.models.Path)20 RefProperty (io.swagger.models.properties.RefProperty)19 StringProperty (io.swagger.models.properties.StringProperty)18 ArrayProperty (io.swagger.models.properties.ArrayProperty)13 HeaderParameter (io.swagger.models.parameters.HeaderParameter)12 Response (io.swagger.models.Response)10 SerializableParameter (io.swagger.models.parameters.SerializableParameter)10 ArrayList (java.util.ArrayList)9