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.

swagger-parser-v3 fails to generate spec on relative server urls on Windows

See original GitHub issue

After a change in OpenAPIDeserializer, swagger-parser-v3 fails to generate any model for a spec with a relative server url when running on Windows.

The message recorded in result is “begin 0, end -1, length 3”, quite unhelpfully, while the result of new OpenAPIParser().readLocation(inputSpec, authorizationValues, options) is just result.openAPI and result.messages both being null

According to the OpenAPI spec relative base paths are supported by the OpenAPI standard. Relative URLs are of course very useful when developing a backend-for-frontend API. At any rate, if swagger-parser was to not support it, users deserve a better error message.

Here is a minimum OpenAPI spec to reproduce the problem:

openapi: 3.0.2
info:
  title: Sample API
  description: A small example to demonstrate individual problems
  version: 0.1.9
servers:
  - url: /v1
    description: Server
paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        '200':
          description: Updated
components:
  schemas:
    Pet:
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
        name:
          type: string
        birth_date:
          type: string
          format: date

As of now, I’m executing the parser through openapi-generator, so I don’t have a targeted test case or pull request, but can work on this if you want.

Cause:

Trying to substitute server variable values when there are no variables causes a StringIndexOutOfBoundsException. This line should’ve checked on value.indexOf("{") before doing substring. Additionally, the code does not support multiple template variables as required by the spec. Please not that the code here uses tabs and spaces inconsistently and looks bad on github.

The substitution only happens on URIParseException, which is caused by Windows-style paths with "" instead of “/”.

The exception is caught in OpenAPIDeserializer.deserialize, with result.setMessages(Arrays.asList(e.getMessage()));. I suggest replacing this with result.setMessages(Arrays.asList(e.toString()));, which would have given the slightly more helpful java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 4 in this case. Or perhaps the exception should be allowed to propagate further?

In my case, the code returns to OpenAPIParser which continues with the next extension (first was OpenAPI, second is Swagger). This means that as OpenAPIParser.readLocation returns even the meager message “begin 0, end -1, length 4” is lost.

Suggested fix

  1. Replace the code for variable checking in io.swagger.v3.parser.util.OpenAPIDeserializer#getServer
  2. Remove catch block in io.swagger.v3.parser.util.OpenAPIDeserializer#deserialize, letting fatal bugs messages propagate to the user instead of causing silent errors. Alternatively, use e.toString() instead of e.getMessage() to get less cryptic errors
  3. If the previous suggestion is not followed, change OpenAPIParser to return immediately if output.getMessages() != null so that errors during processing of OpenAPI aren’t overwritten by the Swagger parser

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jhannescommented, May 19, 2021

@willemsendennis I’ve submitted a pull request. It looks like I misclassified the core issue - it was resolution of Windows path names

1reaction
jhannescommented, May 19, 2021

I will be able to make a pull request, but not immediately 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Release notes & updates – Azure CLI - Microsoft Learn
Learn about the latest Azure Command-Line Interface (CLI) release notes and updates for both the current and beta versions of the CLI.
Read more >
swagger-ui-express - npm
This module allows you to serve auto-generated swagger-ui generated API docs from express, based on a swagger.json file.
Read more >
Descriptor Format of Catalog Entities - Backstage.io
This section describes the default data shape and semantics of catalog entities. This both applies to objects given to and returned from the...
Read more >
Swagger Codegen Documentation
The Swagger Codegen is an open source code-generator to build server stubs and client SDKs directly from a Swagger defined RESTful API. The...
Read more >
SwaggerParser class | Swagger Parser - API Dev Tools
spec option is ignored if your API is in OpenAPI 3.0 format. If validation fails, then an error will be passed to the...
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