Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding form parameters via RequestBody on Method Level #3644

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -718,6 +718,31 @@ protected void processRequestBody(Parameter requestBodyParameter, Operation oper
//requestBody.setExtensions(extensions);
operation.setRequestBody(requestBody);
}
} else if (requestBodyParameter.getSchema() != null) {
// This allows us to use RequestBody on the method for encoding attributes
// The particular methodology employed here means that all attributes
// outside of the schema for application/x-www-form-urlencoded are carried over.
boolean formParams = false;
if(methodConsumes != null) {
for (String methodConsume : methodConsumes.value()) {
if ("application/x-www-form-urlencoded".equalsIgnoreCase(methodConsume)) {
formParams = true;
break;
}
}
}
if (!formParams && classConsumes != null) {
for (String classConsume : classConsumes.value()) {
if ("application/x-www-form-urlencoded".equalsIgnoreCase(classConsume)) {
formParams = true;
break;
}
}
}
if(formParams) {
Content content = operation.getRequestBody().getContent();
content.get("application/x-www-form-urlencoded").schema(requestBodyParameter.getSchema());
}
}
}
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import io.swagger.v3.jaxrs2.it.resources.MultiPartFileResource;
import io.swagger.v3.jaxrs2.it.resources.OctetStreamResource;
import io.swagger.v3.jaxrs2.it.resources.UrlEncodedResource;
import io.swagger.v3.jaxrs2.it.resources.UrlEncodedResourceWithEncodings;
import io.swagger.v3.jaxrs2.resources.model.Pet;
import io.swagger.v3.jaxrs2.resources.model.User;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -520,4 +521,69 @@ public void testMultiPart() throws IOException {
compareAsYaml(MultiPartFileResource.class, expectedYAML);
}

@Test
public void testUrlEncodedWithEncoding() throws IOException {
String expectedYAML = "openapi: 3.0.1\n" +
"paths:\n" +
" /things/search:\n" +
" post:\n" +
" operationId: searchForThings\n" +
" requestBody:\n" +
" content:\n" +
" application/x-www-form-urlencoded:\n" +
" schema:\n" +
" type: object\n" +
" properties:\n" +
" id:\n" +
" type: array\n" +
" items:\n" +
" type: string\n" +
" name:\n" +
" type: array\n" +
" items:\n" +
" type: string\n" +
" encoding:\n" +
" id:\n" +
" style: form\n" +
" explode: true\n" +
" name:\n" +
" style: form\n" +
" explode: true\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" application/json: {}\n" +
" /things/sriracha:\n" +
" post:\n" +
" operationId: srirachaThing\n" +
" requestBody:\n" +
" content:\n" +
" application/x-www-form-urlencoded:\n" +
" schema:\n" +
" type: object\n" +
" properties:\n" +
" id:\n" +
" type: array\n" +
" items:\n" +
" type: string\n"+
" name:\n" +
" type: array\n" +
" items:\n" +
" type: string\n" +
" encoding:\n" +
" id:\n" +
" style: form\n" +
" explode: true\n" +
" name:\n" +
" style: form\n" +
" explode: true\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" application/json: {}";
;
compareAsYaml(UrlEncodedResourceWithEncodings.class, expectedYAML);
}
}
Expand Up @@ -174,6 +174,98 @@ public class OpenApiResourceIT extends AbstractAnnotationTest {
" }\n" +
" }\n" +
" },\n" +
" \"/things/search\": {\n" +
" \"post\": {\n" +
" \"operationId\": \"searchForThings\",\n" +
" \"requestBody\": {\n"+
" \"content\": {\n"+
" \"application/x-www-form-urlencoded\": {\n"+
" \"schema\":{\n"+
" \"type\":\"object\",\n" +
" \"properties\": {\n" +
" \"id\":{\n"+
" \"type\":\"array\",\n" +
" \"items\":{\n" +
" \"type\":\"string\"\n" +
" }\n" +
" },\n" +
" \"name\":{\n" +
" \"type\":\"array\",\n" +
" \"items\":{\n" +
" \"type\":\"string\"\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"encoding\":{\n" +
" \"id\":{\n" +
" \"style\":\"form\",\n" +
" \"explode\":true" +
" },\n" +
" \"name\":{\n" +
" \"style\":\"form\",\n" +
" \"explode\":true\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"responses\":{\n" +
" \"default\":{\n" +
" \"description\":\"default response\",\n" +
" \"content\":{\n" +
" \"application/json\":{}\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"/things/sriracha\": {\n" +
" \"post\": {\n" +
" \"operationId\": \"srirachaThing\",\n" +
" \"requestBody\": {\n"+
" \"content\": {\n"+
" \"application/x-www-form-urlencoded\": {\n"+
" \"schema\":{\n"+
" \"type\":\"object\",\n" +
" \"properties\": {\n" +
" \"id\":{\n"+
" \"type\":\"array\",\n" +
" \"items\":{\n" +
" \"type\":\"string\"\n" +
" }\n" +
" },\n" +
" \"name\":{\n" +
" \"type\":\"array\",\n" +
" \"items\":{\n" +
" \"type\":\"string\"\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"encoding\":{\n" +
" \"id\":{\n" +
" \"style\":\"form\",\n" +
" \"explode\":true" +
" },\n" +
" \"name\":{\n" +
" \"style\":\"form\",\n" +
" \"explode\":true\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"responses\":{\n" +
" \"default\":{\n" +
" \"description\":\"default response\",\n" +
" \"content\":{\n" +
" \"application/json\":{}\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"/users/add\": {\n" +
" \"post\": {\n" +
" \"operationId\": \"addUser\",\n" +
Expand Down Expand Up @@ -450,6 +542,64 @@ public class OpenApiResourceIT extends AbstractAnnotationTest {
" description: default response\n" +
" content:\n" +
" application/json: {}\n" +
" /things/search:\n" +
" post:\n" +
" operationId: searchForThings\n" +
" requestBody:\n" +
" content:\n" +
" application/x-www-form-urlencoded:\n" +
" schema:\n" +
" type: object\n" +
" properties:\n" +
" id:\n" +
" type: array\n" +
" items:\n" +
" type: string\n" +
" name:\n" +
" type: array\n" +
" items:\n" +
" type: string\n" +
" encoding:\n" +
" id:\n" +
" style: form\n" +
" explode: true\n" +
" name:\n" +
" style: form\n" +
" explode: true\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" application/json: {}\n" +
" /things/sriracha:\n" +
" post:\n" +
" operationId: srirachaThing\n" +
" requestBody:\n" +
" content:\n" +
" application/x-www-form-urlencoded:\n" +
" schema:\n" +
" type: object\n" +
" properties:\n" +
" id:\n" +
" type: array\n" +
" items:\n" +
" type: string\n" +
" name:\n" +
" type: array\n" +
" items:\n" +
" type: string\n" +
" encoding:\n" +
" id:\n" +
" style: form\n" +
" explode: true\n" +
" name:\n" +
" style: form\n" +
" explode: true\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" application/json: {}\n" +
" /users/add:\n" +
" post:\n" +
" operationId: addUser\n" +
Expand Down
@@ -0,0 +1,54 @@
package io.swagger.v3.jaxrs2.it.resources;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterStyle;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Encoding;
import io.swagger.v3.oas.annotations.parameters.RequestBody;

import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

import static io.swagger.v3.oas.annotations.enums.Explode.FALSE;
import static io.swagger.v3.oas.annotations.enums.Explode.TRUE;

@Path("/things")
@Produces("application/json")
public class UrlEncodedResourceWithEncodings {

@POST
@Path("/search")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@RequestBody(content = @Content(encoding = {@Encoding(name = "id", style = "FORM", explode = true),
@Encoding(name = "name", style = "FORM", explode = true)}) )
public Response searchForThings( @BeanParam CompositeFormBody body ) {
return Response.status(200).entity("Searching for something").build();
}

@POST
@Path("/sriracha")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@RequestBody(content = @Content(encoding = {@Encoding(name = "id", style = "FORM", explode = true),
@Encoding(name = "name", style = "FORM", explode = true )}))
public Response srirachaThing(@FormParam( "id" ) List<String> ids, @FormParam( "name" ) List<String> names) {
return Response.status(200).entity("Sriracha!").build();
}

public static class CompositeFormBody {
@Parameter(name = "id", style = ParameterStyle.FORM, explode = TRUE)
@FormParam("id")
public List<String> ids;

@Parameter(name = "name", style = ParameterStyle.FORM, explode = FALSE)
@FormParam("name")
public List<String> names;
}
}