Java Code Examples for io.swagger.v3.oas.models.parameters.RequestBody#content()

The following examples show how to use io.swagger.v3.oas.models.parameters.RequestBody#content() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenApiOperationValidationsTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "getOrHeadWithBodyExpectations")
public void testGetOrHeadWithBodyWithDisabledRecommendations(PathItem.HttpMethod method, String operationId, String ref, Content content, boolean shouldTriggerFailure) {
    RuleConfiguration config = new RuleConfiguration();
    config.setEnableRecommendations(false);
    OpenApiOperationValidations validator = new OpenApiOperationValidations(config);

    Operation op = new Operation().operationId(operationId);
    RequestBody body = new RequestBody();
    if (StringUtils.isNotEmpty(ref) || content != null) {
        body.$ref(ref);
        body.content(content);

        op.setRequestBody(body);
    }

    ValidationResult result = validator.validate(new OperationWrapper(null, op, method));
    Assert.assertNotNull(result.getWarnings());

    List<Invalid> warnings = result.getWarnings().stream()
            .filter(invalid -> "API GET/HEAD defined with request body".equals(invalid.getRule().getDescription()))
            .collect(Collectors.toList());

    Assert.assertNotNull(warnings);
    Assert.assertEquals(warnings.size(), 0, "Expected warnings not to include recommendation.");
}
 
Example 2
Source File: OpenApiOperationValidationsTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "getOrHeadWithBodyExpectations")
public void testGetOrHeadWithBodyWithDisabledRule(PathItem.HttpMethod method, String operationId, String ref, Content content, boolean shouldTriggerFailure) {
    RuleConfiguration config = new RuleConfiguration();
    config.setEnableApiRequestUriWithBodyRecommendation(false);
    OpenApiOperationValidations validator = new OpenApiOperationValidations(config);

    Operation op = new Operation().operationId(operationId);
    RequestBody body = new RequestBody();
    if (StringUtils.isNotEmpty(ref) || content != null) {
        body.$ref(ref);
        body.content(content);

        op.setRequestBody(body);
    }

    ValidationResult result = validator.validate(new OperationWrapper(null, op, method));
    Assert.assertNotNull(result.getWarnings());

    List<Invalid> warnings = result.getWarnings().stream()
            .filter(invalid -> "API GET/HEAD defined with request body".equals(invalid.getRule().getDescription()))
            .collect(Collectors.toList());

    Assert.assertNotNull(warnings);
    Assert.assertEquals(warnings.size(), 0, "Expected warnings not to include recommendation.");
}
 
Example 3
Source File: SchemaGenerator.java    From Poseidon with Apache License 2.0 6 votes vote down vote up
private static void handleParams(Operation operation, ParamPOJO paramPOJO, Path modelsDir, boolean isRequired) {
    if (paramPOJO.isFile()) {
        return;
    }

    if (paramPOJO.isBody()) {
        final RequestBody requestBody = new RequestBody();
        requestBody.required(isRequired);
        final Content content = new Content();
        final MediaType mediaType = new MediaType();
        mediaType.schema(createSchema(paramPOJO, modelsDir));
        content.addMediaType("application/json", mediaType);
        requestBody.content(content);
        operation.requestBody(requestBody);
        return;
    }

    operation.addParametersItem(createParameter(paramPOJO, modelsDir).required(isRequired));
}
 
Example 4
Source File: OpenApiOperationValidationsTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "getOrHeadWithBodyExpectations")
public void testGetOrHeadWithBody(PathItem.HttpMethod method, String operationId, String ref, Content content, boolean shouldTriggerFailure) {
    RuleConfiguration config = new RuleConfiguration();
    config.setEnableRecommendations(true);
    OpenApiOperationValidations validator = new OpenApiOperationValidations(config);

    Operation op = new Operation().operationId(operationId);
    RequestBody body = new RequestBody();
    if (StringUtils.isNotEmpty(ref) || content != null) {
        body.$ref(ref);
        body.content(content);

        op.setRequestBody(body);
    }

    ValidationResult result = validator.validate(new OperationWrapper(null, op, method));
    Assert.assertNotNull(result.getWarnings());

    List<Invalid> warnings = result.getWarnings().stream()
            .filter(invalid -> "API GET/HEAD defined with request body".equals(invalid.getRule().getDescription()))
            .collect(Collectors.toList());

    Assert.assertNotNull(warnings);
    if (shouldTriggerFailure) {
        Assert.assertEquals(warnings.size(), 1, "Expected warnings to include recommendation.");
    } else {
        Assert.assertEquals(warnings.size(), 0, "Expected warnings not to include recommendation.");
    }
}
 
Example 5
Source File: SwaggerConverter.java    From raptor with Apache License 2.0 5 votes vote down vote up
/**
 * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#requestBodyObject
 *
 * @param rpc
 * @return
 */
protected RequestBody getRequestBody(Rpc rpc) {
    RequestBody requestBody = new RequestBody();
    ProtoType protoType = rpc.requestType();
    Type type = schmea.getType(protoType);
    requestBody.description(type.documentation());
    requestBody.required(true);
    requestBody.content(getContent(rpc.requestType()));
    return requestBody;
}
 
Example 6
Source File: SwaggerConverter.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
private RequestBody convertParameterToRequestBody(io.swagger.models.parameters.Parameter param, List<String> consumes) {
    RequestBody body = new RequestBody();
    BodyParameter bp = (BodyParameter) param;

    List<String> mediaTypes = new ArrayList<>(globalConsumes);
    if (consumes != null && consumes.size() > 0) {
        mediaTypes.clear();
        mediaTypes.addAll(consumes);
    }

    if (mediaTypes.size() == 0) {
        mediaTypes.add("*/*");
    }

    if (StringUtils.isNotBlank(param.getDescription())) {
        body.description(param.getDescription());
    }
    body.required(param.getRequired());

    Content content = new Content();
    for (String type : mediaTypes) {
        content.addMediaType(type,
                new MediaType().schema(
                        convert(bp.getSchema())));
        if (StringUtils.isNotBlank(bp.getDescription())) {
            body.setDescription(bp.getDescription());
        }
    }
    convertExamples(((BodyParameter) param).getExamples(), content);
    body.content(content);
    return body;
}
 
Example 7
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 4 votes vote down vote up
private RequestBody createRequestBody(MethodDeclaration methodDeclaration) {
    Map<String, String> paramsDescription = new HashMap<>();
    methodDeclaration.getJavadoc().ifPresent(javadoc -> {
        for (JavadocBlockTag blockTag : javadoc.getBlockTags()) {
            if (blockTag.getType() == JavadocBlockTag.Type.PARAM) {
                paramsDescription.put(blockTag.getName().orElse(""),
                        blockTag.getContent().toText());
            }
        }
    });

    RequestBody requestBody = new RequestBody();
    Content requestBodyContent = new Content();
    requestBody.content(requestBodyContent);
    MediaType requestBodyObject = new MediaType();
    requestBodyContent.addMediaType("application/json", requestBodyObject);
    Schema requestSchema = new ObjectSchema();
    requestSchema.setRequired(new ArrayList<>());
    requestBodyObject.schema(requestSchema);
    methodDeclaration.getParameters().forEach(parameter -> {
        Schema paramSchema = parseTypeToSchema(parameter.getType(), "");
        usedTypes.putAll(collectUsedTypesFromSchema(paramSchema));
        String name = (isReservedWord(parameter.getNameAsString()) ? "_"
                : "").concat(parameter.getNameAsString());
        if (GeneratorUtils.isBlank(paramSchema.get$ref())) {
            paramSchema.description(
                    paramsDescription.remove(parameter.getNameAsString()));
        }
        requestSchema.addProperties(name, paramSchema);
        if (GeneratorUtils.isNotTrue(paramSchema.getNullable())
                && !parameter.isAnnotationPresent(Nullable.class)) {
            requestSchema.addRequiredItem(name);
        }
        paramSchema.setNullable(null);
    });
    if (!paramsDescription.isEmpty()) {
        requestSchema.addExtension(
                EXTENSION_VAADIN_CONNECT_PARAMETERS_DESCRIPTION,
                new LinkedHashMap<>(paramsDescription));
    }
    return requestBody;
}