Search in sources :

Example 11 with Parameter

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

the class RestOperationMeta method init.

public void init(OperationMeta operationMeta) {
    this.operationMeta = operationMeta;
    Swagger swagger = operationMeta.getSchemaMeta().getSwagger();
    Operation operation = operationMeta.getSwaggerOperation();
    this.produces = operation.getProduces();
    if (produces == null) {
        this.produces = swagger.getProduces();
    }
    setAbsolutePath(concatPath(swagger.getBasePath(), operationMeta.getOperationPath()));
    this.createProduceProcessors();
    Method method = operationMeta.getMethod();
    Type[] genericParamTypes = method.getGenericParameterTypes();
    if (genericParamTypes.length != operation.getParameters().size()) {
        throw new Error("Param count is not equal between swagger and method,  path=" + absolutePath);
    }
    // 初始化所有rest param
    for (int idx = 0; idx < genericParamTypes.length; idx++) {
        Parameter parameter = operation.getParameters().get(idx);
        Type genericParamType = genericParamTypes[idx];
        RestParam param = new RestParam(idx, parameter, genericParamType);
        addParam(param);
    }
    this.pathBuilder = new URLPathBuilder(absolutePath, paramMap);
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) Swagger(io.swagger.models.Swagger) Parameter(io.swagger.models.parameters.Parameter) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) Operation(io.swagger.models.Operation) Method(java.lang.reflect.Method)

Example 12 with Parameter

use of io.swagger.models.parameters.Parameter 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)

Example 13 with Parameter

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

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

the class FormDataBodyPartTest method testFormDataBodyPart.

@Test(description = "FormDataBodyPart should be ignored when generating the Swagger document")
public void testFormDataBodyPart() {
    final Swagger swagger = new Reader(new Swagger()).read(ResourceWithFormData.class);
    final List<Parameter> parameters = swagger.getPath("/test/document/{documentName}.json").getPost().getParameters();
    assertEquals(parameters.size(), 3);
    assertEquals(parameters.get(0).getName(), "documentName");
    assertEquals(parameters.get(1).getName(), "input");
    assertEquals(parameters.get(2).getName(), "id");
}
Also used : Swagger(io.swagger.models.Swagger) Reader(io.swagger.jaxrs.Reader) Parameter(io.swagger.models.parameters.Parameter) Test(org.testng.annotations.Test)

Example 15 with Parameter

use of io.swagger.models.parameters.Parameter 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)

Aggregations

Parameter (io.swagger.models.parameters.Parameter)186 BodyParameter (io.swagger.models.parameters.BodyParameter)102 QueryParameter (io.swagger.models.parameters.QueryParameter)101 PathParameter (io.swagger.models.parameters.PathParameter)89 Test (org.testng.annotations.Test)80 HeaderParameter (io.swagger.models.parameters.HeaderParameter)66 Swagger (io.swagger.models.Swagger)63 FormParameter (io.swagger.models.parameters.FormParameter)52 Operation (io.swagger.models.Operation)31 SerializableParameter (io.swagger.models.parameters.SerializableParameter)29 Test (org.junit.Test)25 Type (java.lang.reflect.Type)23 ArrayList (java.util.ArrayList)23 Path (io.swagger.models.Path)19 Annotation (java.lang.annotation.Annotation)17 JavaType (com.fasterxml.jackson.databind.JavaType)14 Response (io.swagger.models.Response)14 Method (java.lang.reflect.Method)14 Property (io.swagger.models.properties.Property)13 ModelImpl (io.swagger.models.ModelImpl)12