Search in sources :

Example 6 with ParameterGenerator

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

the class AbstractOperationGenerator method extractAggregatedParameterGenerators.

protected void extractAggregatedParameterGenerators(Map<String, List<Annotation>> methodAnnotationMap, java.lang.reflect.Parameter methodParameter) {
    JavaType javaType = TypeFactory.defaultInstance().constructType(methodParameter.getParameterizedType());
    BeanDescription beanDescription = Json.mapper().getSerializationConfig().introspect(javaType);
    for (BeanPropertyDefinition propertyDefinition : beanDescription.findProperties()) {
        if (!propertyDefinition.couldSerialize()) {
            continue;
        }
        Annotation[] annotations = collectAnnotations(propertyDefinition);
        ParameterGenerator propertyParameterGenerator = new ParameterGenerator(method, methodAnnotationMap, propertyDefinition.getName(), annotations, propertyDefinition.getPrimaryType());
        parameterGenerators.add(propertyParameterGenerator);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator) Annotation(java.lang.annotation.Annotation)

Example 7 with ParameterGenerator

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

the class PojoOperationGenerator method tryWrapParametersToBody.

private void tryWrapParametersToBody() {
    List<ParameterGenerator> bodyFields = parameterGenerators.stream().filter(pg -> pg.getHttpParameterType() == null).collect(Collectors.toList());
    if (bodyFields.isEmpty()) {
        return;
    }
    if (bodyFields.size() == 1 && SwaggerUtils.isBean(bodyFields.get(0).getGenericType())) {
        ParameterGenerator parameterGenerator = bodyFields.get(0);
        parameterGenerator.setHttpParameterType(HttpParameterType.BODY);
        return;
    }
    // wrap parameters to body
    wrapParametersToBody(bodyFields);
}
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) ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator)

Example 8 with ParameterGenerator

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

the class PojoOperationGenerator method tryWrapParametersToBody.

private void tryWrapParametersToBody() {
    List<ParameterGenerator> bodyFields = parameterGenerators.stream().filter(pg -> pg.getHttpParameterType() == null).collect(Collectors.toList());
    if (bodyFields.isEmpty()) {
        return;
    }
    if (bodyFields.size() == 1 && SwaggerUtils.isBean(bodyFields.get(0).getGenericType())) {
        ParameterGenerator parameterGenerator = bodyFields.get(0);
        parameterGenerator.setHttpParameterType(HttpParameterType.BODY);
        return;
    }
    // wrap parameters to body
    wrapParametersToBody(bodyFields);
}
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) ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator)

Example 9 with ParameterGenerator

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

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 10 with ParameterGenerator

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

the class AbstractOperationGenerator method initMethodParameterGenerators.

protected void initMethodParameterGenerators(Map<String, List<Annotation>> methodAnnotationMap) {
    for (java.lang.reflect.Parameter methodParameter : method.getParameters()) {
        Type genericType = TypeToken.of(clazz).resolveType(methodParameter.getParameterizedType()).getType();
        ParameterGenerator parameterGenerator = new ParameterGenerator(method, methodAnnotationMap, methodParameter, genericType);
        validateParameter(parameterGenerator.getGenericType());
        if (isContextParameter(parameterGenerator.getGenericType())) {
            continue;
        }
        // springmvc: is query, and is bean type
        if (isAggregatedParameter(parameterGenerator, methodParameter)) {
            extractAggregatedParameterGenerators(methodAnnotationMap, methodParameter);
            continue;
        }
        parameterGenerators.add(parameterGenerator);
    }
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) HttpParameterType(org.apache.servicecomb.swagger.generator.core.model.HttpParameterType) JavaType(com.fasterxml.jackson.databind.JavaType) ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator)

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