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

The following examples show how to use io.swagger.v3.oas.models.parameters.RequestBody#setContent() . 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: 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 2
Source File: RequestBodyBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Build content.
 *
 * @param requestBody the request body
 * @param methodAttributes the method attributes
 * @param schema the schema
 * @param content the content
 */
private void buildContent(RequestBody requestBody, MethodAttributes methodAttributes, Schema<?> schema, Content content) {
	for (String value : methodAttributes.getMethodConsumes()) {
		io.swagger.v3.oas.models.media.MediaType mediaTypeObject = new io.swagger.v3.oas.models.media.MediaType();
		mediaTypeObject.setSchema(schema);
		if (content.get(value) != null) {
			mediaTypeObject.setExample(content.get(value).getExample());
			mediaTypeObject.setExamples(content.get(value).getExamples());
			mediaTypeObject.setEncoding(content.get(value).getEncoding());
		}
		content.addMediaType(value, mediaTypeObject);
	}
	requestBody.setContent(content);
}
 
Example 3
Source File: JavaClientCodegenTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test
public void arraysInRequestBody() {
    OpenAPI openAPI = TestUtils.createOpenAPI();
    final JavaClientCodegen codegen = new JavaClientCodegen();
    codegen.setOpenAPI(openAPI);

    RequestBody body1 = new RequestBody();
    body1.setDescription("A list of ids");
    body1.setContent(new Content().addMediaType("application/json", new MediaType().schema(new ArraySchema().items(new StringSchema()))));
    CodegenParameter codegenParameter1 = codegen.fromRequestBody(body1, new HashSet<String>(), null);
    Assert.assertEquals(codegenParameter1.description, "A list of ids");
    Assert.assertEquals(codegenParameter1.dataType, "List<String>");
    Assert.assertEquals(codegenParameter1.baseType, "String");

    RequestBody body2 = new RequestBody();
    body2.setDescription("A list of list of values");
    body2.setContent(new Content().addMediaType("application/json", new MediaType().schema(new ArraySchema().items(new ArraySchema().items(new IntegerSchema())))));
    CodegenParameter codegenParameter2 = codegen.fromRequestBody(body2, new HashSet<String>(), null);
    Assert.assertEquals(codegenParameter2.description, "A list of list of values");
    Assert.assertEquals(codegenParameter2.dataType, "List<List<Integer>>");
    Assert.assertEquals(codegenParameter2.baseType, "List");

    RequestBody body3 = new RequestBody();
    body3.setDescription("A list of points");
    body3.setContent(new Content().addMediaType("application/json", new MediaType().schema(new ArraySchema().items(new ObjectSchema().$ref("#/components/schemas/Point")))));
    ObjectSchema point = new ObjectSchema();
    point.addProperties("message", new StringSchema());
    point.addProperties("x", new IntegerSchema().format(SchemaTypeUtil.INTEGER32_FORMAT));
    point.addProperties("y", new IntegerSchema().format(SchemaTypeUtil.INTEGER32_FORMAT));
    CodegenParameter codegenParameter3 = codegen.fromRequestBody(body3, new HashSet<String>(), null);
    Assert.assertEquals(codegenParameter3.description, "A list of points");
    Assert.assertEquals(codegenParameter3.dataType, "List<Point>");
    Assert.assertEquals(codegenParameter3.baseType, "Point");
}
 
Example 4
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineRequestBody() throws Exception {
    OpenAPI openAPI = new OpenAPI();


    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("street", new StringSchema());

    Schema schema = new Schema();
    schema.addProperties("address", objectSchema);
    schema.addProperties("name", new StringSchema());

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

    Content content = new Content();
    content.addMediaType("*/*", mediaType );

    RequestBody requestBody = new RequestBody();
    requestBody.setContent(content);

    Operation operation = new Operation();
    operation.setRequestBody(requestBody);

    PathItem pathItem = new PathItem();
    pathItem.setGet(operation);
    openAPI.path("/hello",pathItem);

    new InlineModelResolver().flatten(openAPI);

    Operation getOperation = openAPI.getPaths().get("/hello").getGet();
    RequestBody body = getOperation.getRequestBody();
    assertTrue(body.getContent().get("*/*").getSchema().get$ref() != null);

    Schema bodySchema = openAPI.getComponents().getSchemas().get("body");
    assertTrue(bodySchema instanceof Schema);

    assertNotNull(bodySchema.getProperties().get("address"));
}
 
Example 5
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 6
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;
}