Search in sources :

Example 86 with QueryParameter

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

the class QueryVarParamWriterTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("q");
    parameter.setCollectionFormat("csv");
    queryVarParamWriterCsv = new QueryVarParamWriter(new RestParam(parameter, String[].class));
    parameter = new QueryParameter();
    parameter.setName("q");
    parameter.setCollectionFormat("multi");
    queryVarParamWriterMulti = new QueryVarParamWriter(new RestParam(parameter, String[].class));
    parameter = new QueryParameter();
    parameter.setName("q");
    queryVarParamWriterDefault = new QueryVarParamWriter(new RestParam(parameter, String[].class));
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) RestParam(org.apache.servicecomb.common.rest.definition.RestParam) BeforeClass(org.junit.BeforeClass)

Example 87 with QueryParameter

use of io.swagger.models.parameters.QueryParameter in project killbill by killbill.

the class KillBillApiDefinition method decorateOperation.

private void decorateOperation(final Operation op, final String pathName, final String httpMethod) {
    if (op != null) {
        // Bug in swagger ? somehow when we only specify a 201, swagger adds a 200 response with the schema response
        if (httpMethod.equals("POST")) {
            if (op.getResponses().containsKey("201") && op.getResponses().containsKey("200")) {
                final Response resp200 = op.getResponses().remove("200");
                final Response resp201 = op.getResponses().get("201");
                if (resp201.getSchema() == null) {
                    resp201.setSchema(resp200.getSchema());
                }
            }
        }
        op.addSecurity(BASIC_AUTH_SCHEME, null);
        if (requiresTenantInformation(pathName, httpMethod)) {
            op.addSecurity(API_KEY_SCHEME, null);
            op.addSecurity(API_SECRET_SCHEME, null);
        }
        for (Parameter p : op.getParameters()) {
            if (p instanceof BodyParameter) {
                p.setRequired(true);
            } else if (p instanceof PathParameter) {
                p.setRequired(true);
            } else if (p instanceof HeaderParameter) {
                if (p.getName().equals(HDR_CREATED_BY)) {
                    p.setRequired(true);
                }
            } else if (p instanceof QueryParameter) {
                QueryParameter qp = (QueryParameter) p;
                if (qp.getName().equals(QUERY_AUDIT)) {
                    qp.setRequired(false);
                    qp.setType("string");
                    final List<String> values = ImmutableList.copyOf(Iterables.transform(ImmutableList.<AuditLevel>copyOf(AuditLevel.values()), new Function<AuditLevel, String>() {

                        @Override
                        public String apply(final AuditLevel input) {
                            return input.toString();
                        }
                    }));
                    qp.setEnum(values);
                } else if (qp.getName().equals(JaxrsResource.QUERY_REQUESTED_DT) || qp.getName().equals(JaxrsResource.QUERY_ENTITLEMENT_REQUESTED_DT) || qp.getName().equals(JaxrsResource.QUERY_BILLING_REQUESTED_DT) || qp.getName().equals(JaxrsResource.QUERY_ENTITLEMENT_EFFECTIVE_FROM_DT) || qp.getName().equals(JaxrsResource.QUERY_START_DATE) || qp.getName().equals(JaxrsResource.QUERY_END_DATE) || qp.getName().equals(JaxrsResource.QUERY_TARGET_DATE)) {
                    qp.setType("string");
                    // Yack... See #922
                    if (op.getOperationId().equals("getCatalogJson") || op.getOperationId().equals("getCatalogXml") || op.getOperationId().equals("setTestClockTime")) {
                        qp.setFormat("date-time");
                    } else {
                        qp.setFormat("date");
                    }
                }
            }
        }
    }
}
Also used : Response(io.swagger.models.Response) QueryParameter(io.swagger.models.parameters.QueryParameter) AuditLevel(org.killbill.billing.util.api.AuditLevel) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) BodyParameter(io.swagger.models.parameters.BodyParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) PathParameter(io.swagger.models.parameters.PathParameter)

Example 88 with QueryParameter

use of io.swagger.models.parameters.QueryParameter in project endpoints-java by cloudendpoints.

the class SwaggerGenerator method writeApiMethod.

private void writeApiMethod(ApiMethodConfig methodConfig, ApiConfig apiConfig, Swagger swagger, GenerationContext genCtx) throws ApiConfigException {
    Path path = getOrCreatePath(swagger, methodConfig);
    Operation operation = new Operation();
    operation.setOperationId(getOperationId(apiConfig, methodConfig));
    operation.setDescription(methodConfig.getDescription());
    Collection<String> pathParameters = methodConfig.getPathParameters();
    for (ApiParameterConfig parameterConfig : methodConfig.getParameterConfigs()) {
        switch(parameterConfig.getClassification()) {
            case API_PARAMETER:
                boolean isPathParameter = pathParameters.contains(parameterConfig.getName());
                SerializableParameter parameter = isPathParameter ? new PathParameter() : new QueryParameter();
                parameter.setName(parameterConfig.getName());
                parameter.setDescription(parameterConfig.getDescription());
                boolean required = isPathParameter || (!parameterConfig.getNullable() && parameterConfig.getDefaultValue() == null);
                if (parameterConfig.isRepeated()) {
                    TypeToken<?> t = parameterConfig.getRepeatedItemSerializedType();
                    parameter.setType("array");
                    Property p = getSwaggerArrayProperty(t);
                    if (parameterConfig.isEnum()) {
                        // TODO: Not sure if this is the right check
                        ((StringProperty) p).setEnum(getEnumValues(t));
                    }
                    parameter.setItems(p);
                } else if (parameterConfig.isEnum()) {
                    parameter.setType("string");
                    parameter.setEnum(getEnumValues(parameterConfig.getType()));
                    parameter.setRequired(required);
                } else {
                    parameter.setType(TYPE_TO_STRING_MAP.get(parameterConfig.getSchemaBaseType().getType()));
                    parameter.setFormat(TYPE_TO_FORMAT_MAP.get(parameterConfig.getSchemaBaseType().getType()));
                    parameter.setRequired(required);
                }
                operation.parameter(parameter);
                break;
            case RESOURCE:
                TypeToken<?> requestType = parameterConfig.getSchemaBaseType();
                Schema schema = genCtx.schemata.getOrAdd(requestType, apiConfig);
                BodyParameter bodyParameter = new BodyParameter();
                bodyParameter.setName("body");
                bodyParameter.setSchema(new RefModel(schema.name()));
                operation.addParameter(bodyParameter);
                break;
            case UNKNOWN:
                throw new IllegalArgumentException("Unclassifiable parameter type found.");
            case INJECTED:
                // Do nothing, these are synthetic and created by the framework.
                break;
        }
    }
    Response response = new Response().description("A successful response");
    if (methodConfig.hasResourceInResponse()) {
        TypeToken<?> returnType = ApiAnnotationIntrospector.getSchemaType(methodConfig.getReturnType(), apiConfig);
        Schema schema = genCtx.schemata.getOrAdd(returnType, apiConfig);
        response.setSchema(new RefProperty(schema.name()));
    }
    operation.response(200, response);
    writeAuthConfig(swagger, methodConfig, operation);
    if (methodConfig.isApiKeyRequired()) {
        List<Map<String, List<String>>> security = operation.getSecurity();
        // security requirements, add a new one for just the API key.
        if (security != null) {
            for (Map<String, List<String>> securityEntry : security) {
                securityEntry.put(API_KEY, ImmutableList.<String>of());
            }
        } else {
            operation.addSecurity(API_KEY, ImmutableList.<String>of());
        }
        Map<String, SecuritySchemeDefinition> definitions = swagger.getSecurityDefinitions();
        if (definitions == null || !definitions.containsKey(API_KEY)) {
            swagger.securityDefinition(API_KEY, new ApiKeyAuthDefinition(API_KEY_PARAM, In.QUERY));
        }
    }
    path.set(methodConfig.getHttpMethod().toLowerCase(), operation);
    addDefinedMetricCosts(genCtx.limitMetrics, operation, methodConfig.getMetricCosts());
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) Schema(com.google.api.server.spi.config.model.Schema) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) RefProperty(io.swagger.models.properties.RefProperty) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) ByteArrayProperty(io.swagger.models.properties.ByteArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) FloatProperty(io.swagger.models.properties.FloatProperty) DateProperty(io.swagger.models.properties.DateProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) Path(io.swagger.models.Path) ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Response(io.swagger.models.Response) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 89 with QueryParameter

use of io.swagger.models.parameters.QueryParameter in project incubator-servicecomb-java-chassis by apache.

the class QueryCodecTestBase method should_decode.

private void should_decode(HttpServletRequest request, Object decodedValue) {
    Class<?> targetType = decodedValue == null ? Object.class : decodedValue.getClass();
    QueryParameter queryParameter = new QueryParameter();
    queryParameter.setCollectionFormat(codec.getCodecName());
    if (targetType.isArray()) {
        queryParameter.setType(ArrayProperty.TYPE);
    }
    QueryProcessor queryProcessor = new QueryProcessor(queryParameter, TypeFactory.defaultInstance().constructType(targetType));
    Object values = codec.decode(queryProcessor, request);
    assertThat(values).isEqualTo(decodedValue);
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) QueryProcessor(org.apache.servicecomb.common.rest.codec.param.QueryProcessorCreator.QueryProcessor)

Example 90 with QueryParameter

use of io.swagger.models.parameters.QueryParameter in project incubator-servicecomb-java-chassis by apache.

the class TestQueryProcessorCreator method testCreate.

@Test
public void testCreate() {
    ParamValueProcessorCreator creator = ParamValueProcessorCreatorManager.INSTANCE.findValue(QueryProcessorCreator.PARAMTYPE);
    Parameter parameter = new QueryParameter();
    parameter.setName("query");
    ParamValueProcessor processor = creator.create(parameter, String.class);
    Assert.assertEquals(QueryProcessor.class, processor.getClass());
    String result = (String) processor.convertValue("Hello", TypeFactory.defaultInstance().constructType(String.class));
    Assert.assertEquals("Hello", result);
    result = (String) processor.convertValue("", TypeFactory.defaultInstance().constructType(String.class));
    Assert.assertEquals("", result);
    result = (String) processor.convertValue(null, TypeFactory.defaultInstance().constructType(String.class));
    Assert.assertEquals(null, result);
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) Test(org.junit.Test)

Aggregations

QueryParameter (io.swagger.models.parameters.QueryParameter)92 Test (org.testng.annotations.Test)58 Parameter (io.swagger.models.parameters.Parameter)42 PathParameter (io.swagger.models.parameters.PathParameter)34 BodyParameter (io.swagger.models.parameters.BodyParameter)28 Swagger (io.swagger.models.Swagger)25 HeaderParameter (io.swagger.models.parameters.HeaderParameter)15 Test (org.junit.Test)12 Operation (io.swagger.models.Operation)11 FormParameter (io.swagger.models.parameters.FormParameter)11 SerializableParameter (io.swagger.models.parameters.SerializableParameter)10 StringProperty (io.swagger.models.properties.StringProperty)10 HashMap (java.util.HashMap)10 Path (io.swagger.models.Path)9 RefModel (io.swagger.models.RefModel)9 Type (java.lang.reflect.Type)9 BigDecimal (java.math.BigDecimal)9 RefProperty (io.swagger.models.properties.RefProperty)8 Response (io.swagger.models.Response)7 Method (java.lang.reflect.Method)7