Search in sources :

Example 6 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter in project light-rest-4j by networknt.

the class ValidatorTestUtil method floatParam.

public static SerializableParameter floatParam(final boolean required, final BigDecimal min, final BigDecimal max) {
    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("number");
    when(result.getFormat()).thenReturn("float");
    when(result.getRequired()).thenReturn(required);
    when(result.getMinimum()).thenReturn(min);
    when(result.getMaximum()).thenReturn(max);
    return result;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter)

Example 7 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter in project light-rest-4j by networknt.

the class ValidatorTestUtil method stringParam.

public static SerializableParameter stringParam(final boolean required) {
    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("string");
    when(result.getRequired()).thenReturn(required);
    when(result.getMinimum()).thenReturn(null);
    when(result.getMaximum()).thenReturn(null);
    return result;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter)

Example 8 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter in project light-rest-4j by networknt.

the class ValidatorTestUtil method enumeratedArrayParam.

public static SerializableParameter enumeratedArrayParam(final boolean required, final String collectionFormat, final Property items, final String... enumValues) {
    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("array");
    when(result.getCollectionFormat()).thenReturn(collectionFormat);
    when(result.getRequired()).thenReturn(required);
    when(result.getMaxItems()).thenReturn(null);
    when(result.getMinItems()).thenReturn(null);
    when(result.getEnum()).thenReturn(asList(enumValues));
    when(result.getItems()).thenReturn(items);
    return result;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter)

Example 9 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter in project light-rest-4j by networknt.

the class ValidatorTestUtil method arrayParam.

public static SerializableParameter arrayParam(final boolean required, final String collectionFormat, final Integer minItems, final Integer maxItems, final Boolean unique, final Property items) {
    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("array");
    when(result.getCollectionFormat()).thenReturn(collectionFormat);
    when(result.getRequired()).thenReturn(required);
    when(result.getMinItems()).thenReturn(minItems);
    when(result.getMaxItems()).thenReturn(maxItems);
    when(result.isUniqueItems()).thenReturn(unique);
    when(result.getItems()).thenReturn(items);
    return result;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter)

Example 10 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter 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) TreeMap(java.util.TreeMap)

Aggregations

SerializableParameter (io.swagger.models.parameters.SerializableParameter)13 BodyParameter (io.swagger.models.parameters.BodyParameter)7 PathParameter (io.swagger.models.parameters.PathParameter)6 QueryParameter (io.swagger.models.parameters.QueryParameter)6 Parameter (io.swagger.models.parameters.Parameter)5 FormParameter (io.swagger.models.parameters.FormParameter)4 HeaderParameter (io.swagger.models.parameters.HeaderParameter)4 Swagger (io.swagger.models.Swagger)3 AbstractSerializableParameter (io.swagger.models.parameters.AbstractSerializableParameter)3 ArrayProperty (io.swagger.models.properties.ArrayProperty)3 Property (io.swagger.models.properties.Property)3 RefProperty (io.swagger.models.properties.RefProperty)3 StringProperty (io.swagger.models.properties.StringProperty)3 ArrayList (java.util.ArrayList)3 Test (org.testng.annotations.Test)3 Operation (io.swagger.models.Operation)2 Path (io.swagger.models.Path)2 RefModel (io.swagger.models.RefModel)2 Response (io.swagger.models.Response)2 RefParameter (io.swagger.models.parameters.RefParameter)2