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

The following examples show how to use io.swagger.v3.oas.models.parameters.RequestBody#setRequired() . 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: AbstractRequestBuilder.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Apply bean validator annotations.
 *
 * @param requestBody the request body
 * @param annotations the annotations
 * @param isOptional the is optional
 */
public void applyBeanValidatorAnnotations(final RequestBody requestBody, final List<Annotation> annotations, boolean isOptional) {
	Map<String, Annotation> annos = new HashMap<>();
	boolean requestBodyRequired = false;
	if (!CollectionUtils.isEmpty(annotations)) {
		annotations.forEach(annotation -> annos.put(annotation.annotationType().getName(), annotation));
		requestBodyRequired = annotations.stream()
				.filter(annotation -> org.springframework.web.bind.annotation.RequestBody.class.equals(annotation.annotationType()))
				.anyMatch(annotation -> ((org.springframework.web.bind.annotation.RequestBody) annotation).required());
	}
	boolean validationExists = Arrays.stream(ANNOTATIONS_FOR_REQUIRED).anyMatch(annos::containsKey);

	if (validationExists || (!isOptional && requestBodyRequired))
		requestBody.setRequired(true);
	Content content = requestBody.getContent();
	for (MediaType mediaType : content.values()) {
		Schema<?> schema = mediaType.getSchema();
		applyValidationsToSchema(annos, schema);
	}
}
 
Example 2
Source File: OperationsTransformer.java    From spring-openapi with MIT License 5 votes vote down vote up
private RequestBody createRequestBody(Method method, String userDefinedContentType) {
	ParameterNamePair requestBodyParameter = getRequestBody(method);

	if (requestBodyParameter == null) {
		return null;
	}
	if (shouldBeIgnored(requestBodyParameter.getParameter())) {
		logger.info("Ignoring parameter {}", requestBodyParameter.getName());
		return null;
	}

	Content content = new Content();
	content.addMediaType(resolveContentType(userDefinedContentType, requestBodyParameter.getParameter()),
			schemaGeneratorHelper.createMediaType(
					requestBodyParameter.getParameter().getType(),
					requestBodyParameter.getName(),
					singletonList(getGenericParam(requestBodyParameter.getParameter()))
			)
	);

	RequestBody requestBody = new RequestBody();
	requestBody.setRequired(true);
	requestBody.setContent(content);
	requestBody.setDescription("requestBody");

	requestBodyInterceptors.forEach(interceptor ->
			interceptor.intercept(method, requestBodyParameter.getParameter(), requestBodyParameter.getName(), requestBody)
	);

	return requestBody;
}
 
Example 3
Source File: OAS3Parser.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Construct openAPI definition for graphQL. Add get and post operations
 *
 * @param openAPI OpenAPI
 * @return modified openAPI for GraphQL
 */
private void modifyGraphQLSwagger(OpenAPI openAPI) {
    SwaggerData.Resource resource = new SwaggerData.Resource();
    resource.setAuthType(APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN);
    resource.setPolicy(APIConstants.DEFAULT_SUB_POLICY_UNLIMITED);
    resource.setPath("/");
    resource.setVerb(APIConstants.HTTP_POST);
    Operation postOperation = createOperation(resource);

    //post operation
    RequestBody requestBody = new RequestBody();
    requestBody.setDescription("Query or mutation to be passed to graphQL API");
    requestBody.setRequired(true);

    JSONObject typeOfPayload = new JSONObject();
    JSONObject payload = new JSONObject();
    typeOfPayload.put(APIConstants.TYPE, APIConstants.STRING);
    payload.put(APIConstants.OperationParameter.PAYLOAD_PARAM_NAME, typeOfPayload);

    Schema postSchema = new Schema();
    postSchema.setType(APIConstants.OBJECT);
    postSchema.setProperties(payload);

    MediaType mediaType = new MediaType();
    mediaType.setSchema(postSchema);

    Content content = new Content();
    content.addMediaType(APPLICATION_JSON_MEDIA_TYPE, mediaType);
    requestBody.setContent(content);
    postOperation.setRequestBody(requestBody);

    //add post and get operations to path /*
    PathItem pathItem = new PathItem();
    pathItem.setPost(postOperation);
    Paths paths = new Paths();
    paths.put("/", pathItem);

    openAPI.setPaths(paths);
}
 
Example 4
Source File: OpenAPIDeserializer.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
protected RequestBody getRequestBody(ObjectNode node, String location, ParseResult result) {
    if (node == null){
        return null;
    }
    final RequestBody body = new RequestBody();


    JsonNode ref = node.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                body.set$ref(mungedRef);
            }else{
                body.set$ref(ref.textValue());
            }
            return body;
        } else {
            result.invalidType(location, "$ref", "string", node);
            return null;
        }
    }




    final String description = getString("description", node, false, location, result);
    if (StringUtils.isNotBlank(description)) {
        body.setDescription(description);
    }

    final Boolean required = getBoolean("required", node, false, location, result);
    if(required != null) {
        body.setRequired(required);
    }

    final ObjectNode contentNode = getObject("content", node, true, location, result);
    if (contentNode != null) {
        body.setContent(getContent(contentNode, location + ".content", result));
    }

    Map <String,Object> extensions = getExtensions(node);
    if(extensions != null && extensions.size() > 0) {
        body.setExtensions(extensions);
    }

    Set<String> keys = getKeys(node);
    for(String key : keys) {
        if(!REQUEST_BODY_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }

    return body;
}