Search in sources :

Example 11 with ParameterGenerator

use of org.apache.servicecomb.swagger.generator.ParameterGenerator in project incubator-servicecomb-java-chassis by apache.

the class AbstractOperationGenerator method scanMethodParameters.

protected void scanMethodParameters() {
    initParameterGenerators();
    Set<String> names = new HashSet<>();
    for (ParameterGenerator parameterGenerator : parameterGenerators) {
        scanMethodParameter(parameterGenerator);
        if (!names.add(parameterGenerator.getParameterName())) {
            throw new IllegalStateException(String.format("not support duplicated parameter, name=%s.", parameterGenerator.getParameterName()));
        }
        swaggerOperation.addParameter(parameterGenerator.getGeneratedParameter());
    }
}
Also used : ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator)

Example 12 with ParameterGenerator

use of org.apache.servicecomb.swagger.generator.ParameterGenerator in project incubator-servicecomb-java-chassis by apache.

the class PojoOperationGenerator method wrapParametersToBody.

private void wrapParametersToBody(List<ParameterGenerator> bodyFields) {
    String simpleRef = MethodUtils.findSwaggerMethodName(method) + "Body";
    bodyModel = new ModelImpl();
    bodyModel.setType(ModelImpl.OBJECT);
    for (ParameterGenerator parameterGenerator : bodyFields) {
        // to collect all information by swagger mechanism
        // must have a parameter type
        // but all these parameters will be wrap to be one body parameter, their parameter type must be null
        // so we first set to be BODY, after collected, set back to be null
        parameterGenerator.setHttpParameterType(HttpParameterType.BODY);
        scanMethodParameter(parameterGenerator);
        Property property = ModelConverters.getInstance().readAsProperty(parameterGenerator.getGenericType());
        property.setDescription(parameterGenerator.getGeneratedParameter().getDescription());
        bodyModel.addProperty(parameterGenerator.getParameterName(), property);
        parameterGenerator.setHttpParameterType(null);
    }
    swagger.addDefinition(simpleRef, bodyModel);
    SwaggerGeneratorFeature feature = swaggerGenerator.getSwaggerGeneratorFeature();
    // if not care for this, then can just delete all logic about EXT_JAVA_CLASS/EXT_JAVA_INTF
    if (feature.isExtJavaClassInVendor() && bodyFields.size() > 1 && StringUtils.isNotEmpty(feature.getPackageName())) {
        bodyModel.getVendorExtensions().put(SwaggerConst.EXT_JAVA_CLASS, feature.getPackageName() + "." + simpleRef);
    }
    RefModel refModel = new RefModel();
    refModel.setReference("#/definitions/" + simpleRef);
    bodyParameter = new BodyParameter();
    bodyParameter.name(simpleRef);
    bodyParameter.setSchema(refModel);
    bodyParameter.setName(parameterGenerators.size() == 1 ? parameterGenerators.get(0).getParameterName() : simpleRef);
    List<ParameterGenerator> newParameterGenerators = new ArrayList<>();
    newParameterGenerators.add(new ParameterGenerator(bodyParameter.getName(), Collections.emptyList(), null, HttpParameterType.BODY, bodyParameter));
    parameterGenerators.stream().filter(p -> p.getHttpParameterType() != null).forEach(p -> newParameterGenerators.add(p));
    parameterGenerators = newParameterGenerators;
}
Also used : ModelConverters(io.swagger.converter.ModelConverters) Swagger(io.swagger.models.Swagger) HttpParameterType(org.apache.servicecomb.swagger.generator.core.model.HttpParameterType) SwaggerUtils(org.apache.servicecomb.swagger.SwaggerUtils) ModelImpl(io.swagger.models.ModelImpl) StringUtils(org.apache.commons.lang3.StringUtils) HttpMethod(javax.ws.rs.HttpMethod) ArrayList(java.util.ArrayList) Map(java.util.Map) SwaggerGeneratorFeature(org.apache.servicecomb.swagger.generator.SwaggerGeneratorFeature) AbstractOperationGenerator(org.apache.servicecomb.swagger.generator.core.AbstractOperationGenerator) JavaType(com.fasterxml.jackson.databind.JavaType) Method(java.lang.reflect.Method) Property(io.swagger.models.properties.Property) RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter) AbstractSwaggerGenerator(org.apache.servicecomb.swagger.generator.core.AbstractSwaggerGenerator) Parameter(io.swagger.models.parameters.Parameter) MethodUtils(org.apache.servicecomb.swagger.generator.core.utils.MethodUtils) Collectors(java.util.stream.Collectors) List(java.util.List) ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator) SwaggerConst(org.apache.servicecomb.swagger.generator.SwaggerConst) Annotation(java.lang.annotation.Annotation) Collections(java.util.Collections) SwaggerGeneratorFeature(org.apache.servicecomb.swagger.generator.SwaggerGeneratorFeature) RefModel(io.swagger.models.RefModel) ArrayList(java.util.ArrayList) ModelImpl(io.swagger.models.ModelImpl) ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator) BodyParameter(io.swagger.models.parameters.BodyParameter) Property(io.swagger.models.properties.Property)

Aggregations

ParameterGenerator (org.apache.servicecomb.swagger.generator.ParameterGenerator)12 JavaType (com.fasterxml.jackson.databind.JavaType)8 Annotation (java.lang.annotation.Annotation)6 HttpParameterType (org.apache.servicecomb.swagger.generator.core.model.HttpParameterType)6 ModelConverters (io.swagger.converter.ModelConverters)4 ModelImpl (io.swagger.models.ModelImpl)4 RefModel (io.swagger.models.RefModel)4 Swagger (io.swagger.models.Swagger)4 BodyParameter (io.swagger.models.parameters.BodyParameter)4 Parameter (io.swagger.models.parameters.Parameter)4 Property (io.swagger.models.properties.Property)4 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 List (java.util.List)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 HttpMethod (javax.ws.rs.HttpMethod)4 StringUtils (org.apache.commons.lang3.StringUtils)4 SwaggerUtils (org.apache.servicecomb.swagger.SwaggerUtils)4