Skip to content
New issue

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

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

Already on GitHub? # to your account

Mark form parameters as required when body is required #331

Merged
merged 2 commits into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2033,13 +2033,17 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
for (String propertyName : propertyMap.keySet()) {
CodegenParameter formParameter = fromParameter(new Parameter()
.name(propertyName)
.required(body.getRequired())
.schema(propertyMap.get(propertyName)), imports);
if (isMultipart) {
formParameter.getVendorExtensions().put(CodegenConstants.IS_MULTIPART_EXT_NAME, Boolean.TRUE);
}
// todo: this segment is only to support the "older" template design. it should be removed once all templates are updated with the new {{#contents}} tag.
formParameter.getVendorExtensions().put(CodegenConstants.IS_FORM_PARAM_EXT_NAME, Boolean.TRUE);
formParams.add(formParameter.copy());
if (body.getRequired()) {
requiredParams.add(formParameter.copy());
}
allParams.add(formParameter);

codegenContent.getParameters().add(formParameter.copy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,77 @@ public void testRequestBodyRefConsumesList() {
Assert.assertEquals(codegenOp.consumes.get(1).get("mediaType"), "application/xml");
}

/**
* Tests when a 'application/x-www-form-urlencoded' request body is marked as required that all form
* params are also marked as required.
*
* @see #testOptionalFormParams()
*/
@Test
public void testRequiredFormParams() {
// Setup
final P_DefaultCodegenConfig codegen = new P_DefaultCodegenConfig();

final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/3_0_0/requiredFormParamsTest.yaml");
final String path = "/test_required";

final Operation op = openAPI.getPaths().get(path).getPost();
Assert.assertNotNull(op);

// Test
final CodegenOperation codegenOp = codegen.fromOperation(path, "post", op, openAPI.getComponents().getSchemas(), openAPI);

// Verification
List<CodegenParameter> formParams = codegenOp.getFormParams();
Assert.assertNotNull(formParams);
Assert.assertEquals(formParams.size(), 2);

for (CodegenParameter formParam : formParams) {
Assert.assertTrue(formParam.getRequired(), "Form param '" + formParam.getParamName() + "' is not required.");
}

// Required params must be updated as well.
List<CodegenParameter> requiredParams = codegenOp.getRequiredParams();
Assert.assertNotNull(requiredParams);
Assert.assertEquals(requiredParams.size(), 2);
requiredParams.get(0).getParamName().equals("id");
requiredParams.get(1).getParamName().equals("name");
}

/**
* Tests when a 'application/x-www-form-urlencoded' request body is marked as optional that all form
* params are also marked as optional.
*
* @see #testRequiredFormParams()
*/
@Test
public void testOptionalFormParams() {
// Setup
final P_DefaultCodegenConfig codegen = new P_DefaultCodegenConfig();

final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/3_0_0/requiredFormParamsTest.yaml");
final String path = "/test_optional";

final Operation op = openAPI.getPaths().get(path).getPost();
Assert.assertNotNull(op);

// Test
final CodegenOperation codegenOp = codegen.fromOperation(path, "post", op, openAPI.getComponents().getSchemas(), openAPI);

// Verification
List<CodegenParameter> formParams = codegenOp.getFormParams();
Assert.assertNotNull(formParams);
Assert.assertEquals(formParams.size(), 2);

for (CodegenParameter formParam : formParams) {
Assert.assertFalse(formParam.getRequired(), "Form param '" + formParam.getParamName() + "' is required.");
}

// Required params must be updated as well.
List<CodegenParameter> requiredParams = codegenOp.getRequiredParams();
Assert.assertTrue(requiredParams == null || requiredParams.size() == 0);
}

private static class P_DefaultCodegenConfig extends DefaultCodegenConfig{
@Override
public String getArgumentsLocation() {
Expand Down
44 changes: 44 additions & 0 deletions src/test/resources/3_0_0/requiredFormParamsTest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
openapi: 3.0.0
info:
title: Test Api
version: '3.0.0'

paths:
/test_required:
post:
summary: Operation with form body that is required
operationId: get_with_required_body
requestBody:
required: true
content:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/Category'
responses:
"200":
description: Success

/test_optional:
post:
summary: Operation with form body that is optional
operationId: get_with_optional_body
requestBody:
required: false
content:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/Category'
responses:
"200":
description: Success

components:
schemas:
Category:
type: object
properties:
id:
type: integer
format: int64
name:
type: string