OpenAPIV3Parser assigns null for empty string in response description
See original GitHub issueProblem statement
We have a response definition in the swagger as given below:
"responses": {
"200": {
"description": "",
"headers": {}
}
}
We are executing the below code snippet over this swaggerContent :
// Validate Swagger definition - Line 1
OpenAPI getOpenAPI(String oasDefinition) {
OpenAPIV3Parser openAPIV3Parser = new OpenAPIV3Parser();
SwaggerParseResult parseAttemptForV3 = openAPIV3Parser.readContents(oasDefinition, null, null);
if (CollectionUtils.isNotEmpty(parseAttemptForV3.getMessages())) {
log.debug("Errors found when parsing OAS definition");
}
return parseAttemptForV3.getOpenAPI();
}
OpenAPI openAPI = getOpenAPI(swaggerContent);
Paths paths = openAPI.getPaths();
// some logic here
String swaggerContent = Json.pretty(openAPI);
// Validate swaggerContent - Line 101
The open API object generated is as given below:
responses: class ApiResponses {
{200=class ApiResponse {
description: null
headers: null
content: null
links: null
extensions: null
$ref: null
}}
extensions: null
}
And the returned swagger definition from Json.pretty() method is as given below:
"responses" : {
"200" : { }
},
First, we are validating the swagger definition before applying the logic and there are no errors. But, when revalidating this returned swaggerContent from Json.pretty() method we encounter an error message from OAS3Parser as attribute paths.‘/auth/signup’(post).responses.200.description is missing. We are using OAS Parser version 2.0.20 and it generates definitions in version 3.0.1 - [1].
So the issue is when "description": ""
in the swagger definition the swagger validation is passing. After converting it to an open API object, do some logic and reconvert it back to a swagger content to a string it removes the description attribute from the response, hence throwing an error.
Solution
-
We need to reject the swagger definition when it has a response description is an empty string from Line-1 itself. But here due to the OpenAPIV3Parser behavior, it is rejecting the definition at Line-101.
-
Or else OpenAPIV3Parser should not assign null values for empty strings when converting a string swagger to an open API object
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (5 by maintainers)
@vithu30 @Sarangan0219 Hi we have released parser 🥳
Hi @gracekarina - Sorry, it’s working for me now.
Thanks, Sree