Search in sources :

Example 76 with BodyParameter

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

Example 77 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project swagger-parser by swagger-api.

the class BodyParameterConverterTest method convertInt32BodyParameter.

@Test
public void convertInt32BodyParameter() throws Exception {
    io.swagger.models.apideclaration.Parameter param = new io.swagger.models.apideclaration.Parameter();
    param.setParamType(ParamType.BODY);
    param.setDescription("an int body param");
    param.setRequired(false);
    param.setAllowMultiple(false);
    param.setType("integer");
    param.setFormat(Format.INT32);
    Parameter converted = converter.convertParameter(param);
    assertTrue(converted.getClass().equals(BodyParameter.class));
    BodyParameter bp = (BodyParameter) converted;
    ModelImpl m = (ModelImpl) bp.getSchema();
    assertEquals(param.getType(), m.getType());
    assertEquals(param.getDescription(), bp.getDescription());
    assertEquals((Boolean) param.getRequired(), (Boolean) bp.getRequired());
}
Also used : BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) Test(org.testng.annotations.Test)

Example 78 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project swagger-parser by swagger-api.

the class BodyParameterConverterTest method convertBooleanBodyParameter.

@Test
public void convertBooleanBodyParameter() throws Exception {
    io.swagger.models.apideclaration.Parameter param = new io.swagger.models.apideclaration.Parameter();
    param.setParamType(ParamType.BODY);
    param.setDescription("a boolean body param");
    param.setRequired(false);
    param.setAllowMultiple(false);
    param.setType("boolean");
    Parameter converted = converter.convertParameter(param);
    assertTrue(converted.getClass().equals(BodyParameter.class));
    BodyParameter bp = (BodyParameter) converted;
    ModelImpl m = (ModelImpl) bp.getSchema();
    assertEquals(param.getType(), m.getType());
    assertEquals(param.getDescription(), bp.getDescription());
    assertEquals((Boolean) param.getRequired(), (Boolean) bp.getRequired());
}
Also used : BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) Test(org.testng.annotations.Test)

Example 79 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project swagger-parser by swagger-api.

the class ParameterProcessor method processParameters.

public List<Parameter> processParameters(List<Parameter> parameters) {
    if (parameters == null) {
        return null;
    }
    final List<Parameter> processedPathLevelParameters = new ArrayList<>();
    final List<Parameter> refParameters = new ArrayList<>();
    for (Parameter parameter : parameters) {
        if (parameter instanceof RefParameter) {
            RefParameter refParameter = (RefParameter) parameter;
            final Parameter resolvedParameter = cache.loadRef(refParameter.get$ref(), refParameter.getRefFormat(), Parameter.class);
            if (resolvedParameter == null) {
                // can't resolve it!
                processedPathLevelParameters.add(refParameter);
                continue;
            }
            // if the parameter exists, replace it
            boolean matched = false;
            for (Parameter param : processedPathLevelParameters) {
                if (param.getName().equals(resolvedParameter.getName())) {
                    // ref param wins
                    matched = true;
                    break;
                }
            }
            for (Parameter param : parameters) {
                if (param.getName() != null) {
                    if (param.getName().equals(resolvedParameter.getName())) {
                        // ref param wins
                        matched = true;
                        break;
                    }
                }
            }
            if (resolvedParameter instanceof BodyParameter) {
                BodyParameter bodyParameter = (BodyParameter) resolvedParameter;
                final Model schema = bodyParameter.getSchema();
                modelProcessor.processModel(schema);
            }
            if (matched) {
                refParameters.add(resolvedParameter);
            } else {
                processedPathLevelParameters.add(resolvedParameter);
            }
        } else {
            if (parameter instanceof BodyParameter) {
                BodyParameter bodyParameter = (BodyParameter) parameter;
                final Model schema = bodyParameter.getSchema();
                modelProcessor.processModel(schema);
            }
            processedPathLevelParameters.add(parameter);
        }
    }
    for (Parameter resolvedParameter : refParameters) {
        int pos = 0;
        for (Parameter param : processedPathLevelParameters) {
            if (param.getName().equals(resolvedParameter.getName())) {
                // ref param wins
                processedPathLevelParameters.set(pos, resolvedParameter);
                break;
            }
            pos++;
        }
    }
    return processedPathLevelParameters;
}
Also used : RefParameter(io.swagger.models.parameters.RefParameter) ArrayList(java.util.ArrayList) Model(io.swagger.models.Model) RefParameter(io.swagger.models.parameters.RefParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 80 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project swagger-parser by swagger-api.

the class FileReferenceTests method testIssue421.

@Test
public void testIssue421() {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("./src/test/resources/nested-file-references/issue-421.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getPath("/pet/{petId}"));
    assertNotNull(swagger.getPath("/pet/{petId}").getGet());
    assertNotNull(swagger.getPath("/pet/{petId}").getGet().getParameters());
    assertTrue(swagger.getPath("/pet/{petId}").getGet().getParameters().size() == 1);
    assertTrue(swagger.getPath("/pet/{petId}").getGet().getParameters().get(0).getName().equals("petId"));
    assertTrue(swagger.getDefinitions().get("Pet") instanceof ModelImpl);
    assertTrue(swagger.getDefinitions().get("Pet").getProperties().size() == 6);
    assertNotNull(swagger.getPath("/pet/{petId}").getPost());
    assertNotNull(swagger.getPath("/pet/{petId}").getPost().getParameters());
    assertTrue(swagger.getPath("/pet/{petId}").getPost().getParameters().size() == 3);
    assertTrue(swagger.getPath("/pet/{petId}").getPost().getParameters().get(1) instanceof RefParameter);
    assertTrue(((RefParameter) swagger.getPath("/pet/{petId}").getPost().getParameters().get(1)).getRefFormat() == RefFormat.INTERNAL);
    assertTrue(((RefParameter) swagger.getPath("/pet/{petId}").getPost().getParameters().get(1)).getSimpleRef().equals("name"));
    assertNotNull(swagger.getPath("/store/order"));
    assertNotNull(swagger.getPath("/store/order").getPost());
    assertNotNull(swagger.getPath("/store/order").getPost().getParameters());
    assertTrue(swagger.getPath("/store/order").getPost().getParameters().size() == 1);
    assertTrue(swagger.getPath("/store/order").getPost().getParameters().get(0) instanceof BodyParameter);
    assertNotNull(((BodyParameter) swagger.getPath("/store/order").getPost().getParameters().get(0)).getSchema());
    assertTrue(((BodyParameter) swagger.getPath("/store/order").getPost().getParameters().get(0)).getSchema() instanceof RefModel);
    assertTrue(((RefModel) ((BodyParameter) swagger.getPath("/store/order").getPost().getParameters().get(0)).getSchema()).getSimpleRef().equals("Order"));
    assertTrue(swagger.getDefinitions().get("Order") instanceof ModelImpl);
    assertTrue(swagger.getDefinitions().get("Order").getProperties().size() == 6);
}
Also used : RefParameter(io.swagger.models.parameters.RefParameter) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) BodyParameter(io.swagger.models.parameters.BodyParameter) Test(org.testng.annotations.Test)

Aggregations

BodyParameter (io.swagger.models.parameters.BodyParameter)96 Test (org.testng.annotations.Test)42 Parameter (io.swagger.models.parameters.Parameter)34 Model (io.swagger.models.Model)30 Operation (io.swagger.models.Operation)28 QueryParameter (io.swagger.models.parameters.QueryParameter)26 ModelImpl (io.swagger.models.ModelImpl)23 Swagger (io.swagger.models.Swagger)23 PathParameter (io.swagger.models.parameters.PathParameter)23 RefModel (io.swagger.models.RefModel)22 ArrayModel (io.swagger.models.ArrayModel)21 Property (io.swagger.models.properties.Property)21 Path (io.swagger.models.Path)20 RefProperty (io.swagger.models.properties.RefProperty)19 StringProperty (io.swagger.models.properties.StringProperty)18 ArrayProperty (io.swagger.models.properties.ArrayProperty)13 HeaderParameter (io.swagger.models.parameters.HeaderParameter)12 Response (io.swagger.models.Response)10 SerializableParameter (io.swagger.models.parameters.SerializableParameter)10 ArrayList (java.util.ArrayList)9