question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OpenAPIV3Parser assigns null for empty string in response description

See original GitHub issue

Problem 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

[1] https://github.com/swagger-api/swagger-core/blob/v2.1.2/modules/swagger-models/src/main/java/io/swagger/v3/oas/models/OpenAPI.java#L37

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
gracekarinacommented, Dec 20, 2021

@vithu30 @Sarangan0219 Hi we have released parser 🥳

1reaction
SCSreekanthcommented, Jan 25, 2022

Hi @gracekarina - Sorry, it’s working for me now.

Thanks, Sree

Read more comments on GitHub >

github_iconTop Results From Across the Web

Description with Empty String is not failing #1597 - GitHub
When this document is passed, the parser is not throwing a validation error but returns null for description, and if the model is...
Read more >
How to define a property that can be string or null in OpenAPI ...
I have JSON schema file where one of the properties is defined as either string or null :
Read more >
Use empty string, null or remove empty property in API request ...
I rather like the idea of using an empty string as an easy way to avoid using null.
Read more >
Assigning NULL to string variable in a API call - OutSystems
We are consuming REST API from 3rd party. It is expecting null be send if variable value empty like below. "attribute_for_delete":null.
Read more >
JavaScript Check Empty String – Checking Null or Empty in JS
In this article, you will learn how to check if a sting is empty or null in JavaScript. We will see many examples...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found