Search in sources :

Example 1 with FormParameter

use of io.swagger.models.parameters.FormParameter in project camel by apache.

the class RestSwaggerReader method doParseVerbs.

private void doParseVerbs(Swagger swagger, RestDefinition rest, String camelContextId, List<VerbDefinition> verbs, String pathAsTag) {
    // used during gathering of apis
    List<Path> paths = new ArrayList<>();
    String basePath = rest.getPath();
    for (VerbDefinition verb : verbs) {
        // check if the Verb Definition must be excluded from documentation
        Boolean apiDocs;
        if (verb.getApiDocs() != null) {
            apiDocs = verb.getApiDocs();
        } else {
            // fallback to option on rest
            apiDocs = rest.getApiDocs();
        }
        if (apiDocs != null && !apiDocs) {
            continue;
        }
        // the method must be in lower case
        String method = verb.asVerb().toLowerCase(Locale.US);
        // operation path is a key
        String opPath = SwaggerHelper.buildUrl(basePath, verb.getUri());
        Operation op = new Operation();
        if (ObjectHelper.isNotEmpty(pathAsTag)) {
            // group in the same tag
            op.addTag(pathAsTag);
        }
        final String routeId = verb.getRouteId();
        final String operationId = Optional.ofNullable(rest.getId()).orElse(routeId);
        op.operationId(operationId);
        // add id as vendor extensions
        op.getVendorExtensions().put("x-camelContextId", camelContextId);
        op.getVendorExtensions().put("x-routeId", routeId);
        Path path = swagger.getPath(opPath);
        if (path == null) {
            path = new Path();
            paths.add(path);
        }
        path = path.set(method, op);
        String consumes = verb.getConsumes() != null ? verb.getConsumes() : rest.getConsumes();
        if (consumes != null) {
            String[] parts = consumes.split(",");
            for (String part : parts) {
                op.addConsumes(part);
            }
        }
        String produces = verb.getProduces() != null ? verb.getProduces() : rest.getProduces();
        if (produces != null) {
            String[] parts = produces.split(",");
            for (String part : parts) {
                op.addProduces(part);
            }
        }
        if (verb.getDescriptionText() != null) {
            op.summary(verb.getDescriptionText());
        }
        for (RestOperationParamDefinition param : verb.getParams()) {
            Parameter parameter = null;
            if (param.getType().equals(RestParamType.body)) {
                parameter = new BodyParameter();
            } else if (param.getType().equals(RestParamType.formData)) {
                parameter = new FormParameter();
            } else if (param.getType().equals(RestParamType.header)) {
                parameter = new HeaderParameter();
            } else if (param.getType().equals(RestParamType.path)) {
                parameter = new PathParameter();
            } else if (param.getType().equals(RestParamType.query)) {
                parameter = new QueryParameter();
            }
            if (parameter != null) {
                parameter.setName(param.getName());
                parameter.setDescription(param.getDescription());
                parameter.setRequired(param.getRequired());
                // set type on parameter
                if (parameter instanceof SerializableParameter) {
                    SerializableParameter serializableParameter = (SerializableParameter) parameter;
                    if (param.getDataType() != null) {
                        serializableParameter.setType(param.getDataType());
                        if (param.getDataType().equalsIgnoreCase("array")) {
                            if (param.getArrayType() != null) {
                                if (param.getArrayType().equalsIgnoreCase("string")) {
                                    serializableParameter.setItems(new StringProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("int") || param.getArrayType().equalsIgnoreCase("integer")) {
                                    serializableParameter.setItems(new IntegerProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("long")) {
                                    serializableParameter.setItems(new LongProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("float")) {
                                    serializableParameter.setItems(new FloatProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("double")) {
                                    serializableParameter.setItems(new DoubleProperty());
                                }
                                if (param.getArrayType().equalsIgnoreCase("boolean")) {
                                    serializableParameter.setItems(new BooleanProperty());
                                }
                            }
                        }
                    }
                    if (param.getCollectionFormat() != null) {
                        serializableParameter.setCollectionFormat(param.getCollectionFormat().name());
                    }
                    if (param.getAllowableValues() != null && !param.getAllowableValues().isEmpty()) {
                        serializableParameter.setEnum(param.getAllowableValues());
                    }
                }
                // set default value on parameter
                if (parameter instanceof AbstractSerializableParameter) {
                    AbstractSerializableParameter qp = (AbstractSerializableParameter) parameter;
                    if (param.getDefaultValue() != null) {
                        qp.setDefaultValue(param.getDefaultValue());
                    }
                }
                // set schema on body parameter
                if (parameter instanceof BodyParameter) {
                    BodyParameter bp = (BodyParameter) parameter;
                    if (verb.getType() != null) {
                        if (verb.getType().endsWith("[]")) {
                            String typeName = verb.getType();
                            typeName = typeName.substring(0, typeName.length() - 2);
                            Property prop = modelTypeAsProperty(typeName, swagger);
                            if (prop != null) {
                                ArrayModel arrayModel = new ArrayModel();
                                arrayModel.setItems(prop);
                                bp.setSchema(arrayModel);
                            }
                        } else {
                            String ref = modelTypeAsRef(verb.getType(), swagger);
                            if (ref != null) {
                                bp.setSchema(new RefModel(ref));
                            }
                        }
                    }
                }
                op.addParameter(parameter);
            }
        }
        // if we have an out type then set that as response message
        if (verb.getOutType() != null) {
            Response response = new Response();
            Property prop = modelTypeAsProperty(verb.getOutType(), swagger);
            response.setSchema(prop);
            response.setDescription("Output type");
            op.addResponse("200", response);
        }
        // enrich with configured response messages from the rest-dsl
        doParseResponseMessages(swagger, verb, op);
        // add path
        swagger.path(opPath, path);
    }
}
Also used : AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) SerializableParameter(io.swagger.models.parameters.SerializableParameter) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) ArrayList(java.util.ArrayList) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) FloatProperty(io.swagger.models.properties.FloatProperty) RestOperationParamDefinition(org.apache.camel.model.rest.RestOperationParamDefinition) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) FloatProperty(io.swagger.models.properties.FloatProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) Path(io.swagger.models.Path) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) Response(io.swagger.models.Response) LongProperty(io.swagger.models.properties.LongProperty) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) SerializableParameter(io.swagger.models.parameters.SerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) FormParameter(io.swagger.models.parameters.FormParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) DoubleProperty(io.swagger.models.properties.DoubleProperty) ArrayModel(io.swagger.models.ArrayModel)

Example 2 with FormParameter

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

the class SwaggerJerseyJaxrs method extractParameters.

@Override
public List<Parameter> extractParameters(List<Annotation> annotations, Type type, Set<Type> typesToSkip, Iterator<SwaggerExtension> chain) {
    if (shouldIgnoreType(type, typesToSkip)) {
        // stop the processing chain
        return Collections.emptyList();
    }
    for (Annotation annotation : annotations) {
        if (annotation instanceof FormDataParam) {
            final FormDataParam fd = (FormDataParam) annotation;
            final Class<?> cls = TypeFactory.defaultInstance().constructType(type).getRawClass();
            final Parameter param;
            if (java.io.InputStream.class.isAssignableFrom(cls)) {
                param = new FormParameter().type("file").name(fd.value());
            } else {
                FormParameter fp = new FormParameter().name(fd.value());
                Property schema = ModelConverters.getInstance().readAsProperty(type);
                if (schema != null) {
                    fp.setProperty(schema);
                }
                param = fp;
            }
            return Collections.singletonList(param);
        }
    }
    return super.extractParameters(annotations, type, typesToSkip, chain);
}
Also used : FormParameter(io.swagger.models.parameters.FormParameter) Parameter(io.swagger.models.parameters.Parameter) FormDataParam(com.sun.jersey.multipart.FormDataParam) FormParameter(io.swagger.models.parameters.FormParameter) Property(io.swagger.models.properties.Property) Annotation(java.lang.annotation.Annotation)

Example 3 with FormParameter

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

the class SwaggerJersey2Jaxrs method extractParameters.

@Override
public List<Parameter> extractParameters(final List<Annotation> annotations, final Type type, final Set<Type> typesToSkip, final Iterator<SwaggerExtension> chain) {
    List<Parameter> parameters = new ArrayList<Parameter>();
    if (shouldIgnoreType(type, typesToSkip)) {
        return parameters;
    }
    for (final Annotation annotation : annotations) {
        // just handle the jersey specific annotation
        if (annotation instanceof FormDataParam) {
            FormDataParam fd = (FormDataParam) annotation;
            if (java.io.InputStream.class.isAssignableFrom(constructType(type).getRawClass())) {
                final Parameter param = new FormParameter().type("file").name(fd.value());
                parameters.add(param);
            } else {
                final FormParameter fp = new FormParameter().name(fd.value());
                final Property schema = ModelConverters.getInstance().readAsProperty(type);
                if (schema != null) {
                    fp.setProperty(schema);
                }
                parameters.add(fp);
            }
        }
    }
    // Only call down to the other items in the chain if no parameters were produced
    if (parameters.isEmpty()) {
        parameters = super.extractParameters(annotations, type, typesToSkip, chain);
    }
    return parameters;
}
Also used : ArrayList(java.util.ArrayList) FormParameter(io.swagger.models.parameters.FormParameter) Parameter(io.swagger.models.parameters.Parameter) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) FormParameter(io.swagger.models.parameters.FormParameter) Property(io.swagger.models.properties.Property) Annotation(java.lang.annotation.Annotation)

Example 4 with FormParameter

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

the class ReaderTest method scanImplicitWithFile.

@Test(description = "scan implicit params with file objct")
public void scanImplicitWithFile() {
    Swagger swagger = getSwagger(ResourceWithImplicitFileParam.class);
    Parameter param = swagger.getPath("/testString").getPost().getParameters().get(0);
    assertTrue(param instanceof FormParameter);
    FormParameter fp = (FormParameter) param;
    assertEquals("file", fp.getType());
}
Also used : Swagger(io.swagger.models.Swagger) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) FormParameter(io.swagger.models.parameters.FormParameter) Test(org.testng.annotations.Test)

Example 5 with FormParameter

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

the class DefaultParameterExtension method extractParameters.

@Override
public List<Parameter> extractParameters(List<Annotation> annotations, Type type, Set<Type> typesToSkip, Iterator<SwaggerExtension> chain) {
    if (shouldIgnoreType(type, typesToSkip)) {
        return new ArrayList<Parameter>();
    }
    List<Parameter> parameters = new ArrayList<Parameter>();
    Parameter parameter = null;
    for (Annotation annotation : annotations) {
        if (annotation instanceof QueryParam) {
            QueryParam param = (QueryParam) annotation;
            QueryParameter qp = new QueryParameter().name(param.value());
            Property schema = createProperty(type);
            if (schema != null) {
                qp.setProperty(schema);
            }
            parameter = qp;
        } else if (annotation instanceof PathParam) {
            PathParam param = (PathParam) annotation;
            PathParameter pp = new PathParameter().name(param.value());
            Property schema = createProperty(type);
            if (schema != null) {
                pp.setProperty(schema);
            }
            parameter = pp;
        } else if (annotation instanceof HeaderParam) {
            HeaderParam param = (HeaderParam) annotation;
            HeaderParameter hp = new HeaderParameter().name(param.value());
            Property schema = createProperty(type);
            if (schema != null) {
                hp.setProperty(schema);
            }
            parameter = hp;
        } else if (annotation instanceof CookieParam) {
            CookieParam param = (CookieParam) annotation;
            CookieParameter cp = new CookieParameter().name(param.value());
            Property schema = createProperty(type);
            if (schema != null) {
                cp.setProperty(schema);
            }
            parameter = cp;
        } else if (annotation instanceof FormParam) {
            FormParam param = (FormParam) annotation;
            FormParameter fp = new FormParameter().name(param.value());
            Property schema = createProperty(type);
            if (schema != null) {
                fp.setProperty(schema);
            }
            parameter = fp;
        } else {
            handleAdditionalAnnotation(parameters, annotation, type, typesToSkip);
        }
    }
    if (parameter != null) {
        parameters.add(parameter);
    }
    return parameters;
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParam(javax.ws.rs.HeaderParam) ArrayList(java.util.ArrayList) PathParameter(io.swagger.models.parameters.PathParameter) FormParameter(io.swagger.models.parameters.FormParameter) Annotation(java.lang.annotation.Annotation) CookieParam(javax.ws.rs.CookieParam) QueryParam(javax.ws.rs.QueryParam) HeaderParameter(io.swagger.models.parameters.HeaderParameter) CookieParameter(io.swagger.models.parameters.CookieParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) CookieParameter(io.swagger.models.parameters.CookieParameter) PathParam(javax.ws.rs.PathParam) HeaderParameter(io.swagger.models.parameters.HeaderParameter) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) FormParam(javax.ws.rs.FormParam)

Aggregations

FormParameter (io.swagger.models.parameters.FormParameter)8 Parameter (io.swagger.models.parameters.Parameter)8 HeaderParameter (io.swagger.models.parameters.HeaderParameter)6 PathParameter (io.swagger.models.parameters.PathParameter)5 QueryParameter (io.swagger.models.parameters.QueryParameter)5 Property (io.swagger.models.properties.Property)4 Annotation (java.lang.annotation.Annotation)4 BodyParameter (io.swagger.models.parameters.BodyParameter)3 ArrayList (java.util.ArrayList)3 Test (org.testng.annotations.Test)3 Swagger (io.swagger.models.Swagger)2 ArrayProperty (io.swagger.models.properties.ArrayProperty)2 RefProperty (io.swagger.models.properties.RefProperty)2 StringProperty (io.swagger.models.properties.StringProperty)2 Type (java.lang.reflect.Type)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)1 FormDataParam (com.sun.jersey.multipart.FormDataParam)1 SwaggerJersey2Jaxrs (io.swagger.jersey.SwaggerJersey2Jaxrs)1 ArrayModel (io.swagger.models.ArrayModel)1