Skip to content

Commit

Permalink
Merge pull request #1645 from swagger-api/issue1644
Browse files Browse the repository at this point in the history
improves AllowEmptyString option validation
  • Loading branch information
gracekarina authored Feb 2, 2022
2 parents d29f6f0 + ca1a014 commit fc0283f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,6 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
return null;
}


Parameter parameter = null;

JsonNode ref = obj.get("$ref");
Expand Down Expand Up @@ -1589,11 +1588,10 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul

String value = getString("in", obj, true, location, result);

if (!result.isAllowEmptyStrings() && value == null) {
if (!result.isAllowEmptyStrings() && StringUtils.isBlank(value) || result.isAllowEmptyStrings() && value == null) {
return null;
}


if (QUERY_PARAMETER.equals(value)) {
parameter = new QueryParameter();
} else if (HEADER_PARAMETER.equals(value)) {
Expand All @@ -1609,7 +1607,6 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
return null;
}


parameter.setIn(value);

value = getString("name", obj, true, location, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,58 @@ public class OpenAPIV3ParserTest {
protected int serverPort = getDynamicPort();
protected WireMockServer wireMockServer;

@Test
public void testIssue1644_NullValue() throws Exception{
ParseOptions options = new ParseOptions();
String issue1644 = "openapi: 3.0.0\n" +
"info:\n" +
" title: Operations\n" +
" version: 0.0.0\n" +
"paths:\n" +
" \"/operations\":\n" +
" post:\n" +
" parameters:\n" +
" - name: param0\n" +
" schema:\n" +
" type: string\n" +
" responses:\n" +
" default:\n" +
" description: None\n";
SwaggerParseResult result = new OpenAPIV3Parser().readContents(issue1644, null, options);

Assert.assertNotNull(result);
Assert.assertNotNull(result.getOpenAPI());
assertEquals(result.getMessages().size(),1);
assertTrue(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].in is missing"));
assertFalse(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].in is not of type `string`"));
}

@Test
public void testIssue1644_EmptyValue() throws Exception{
ParseOptions options = new ParseOptions();
String issue1644 = "openapi: 3.0.0\n" +
"info:\n" +
" title: Operations\n" +
" version: 0.0.0\n" +
"paths:\n" +
" \"/operations\":\n" +
" post:\n" +
" parameters:\n" +
" - name: param0\n" +
" in: ''\n" +
" schema:\n" +
" type: string\n" +
" responses:\n" +
" default:\n" +
" description: None\n";
SwaggerParseResult result = new OpenAPIV3Parser().readContents(issue1644, null, options);

Assert.assertNotNull(result);
Assert.assertNotNull(result.getOpenAPI());
assertEquals(result.getMessages().size(),1);
assertTrue(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].in is not of type `string`"));
}


@Test
public void testEmptyStrings_False() throws Exception{
Expand Down

0 comments on commit fc0283f

Please sign in to comment.