Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

YAML errors on config files are shadowed by "false" JSON errors #3911

Closed
adagios opened this issue Mar 11, 2021 · 3 comments
Closed

YAML errors on config files are shadowed by "false" JSON errors #3911

adagios opened this issue Mar 11, 2021 · 3 comments

Comments

@adagios
Copy link
Contributor

adagios commented Mar 11, 2021

With the swagger-maven-plugin, when using either openapiFilePath or configurationFilePath with YAML files, if there are errors with the YAML a "false" JSON error is shown instead.

Example:

[ERROR] Failed to execute goal io.swagger.core.v3:swagger-maven-plugin:2.1.7:resolve (default) on project recordm-rest: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'openapi': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
[ERROR]  at [Source: (String)"openapi: 3.0.1
[ERROR] info:
[ERROR]   title: RecordM API
[ERROR]   description:
[ERROR]     The RecordM REST API<br/><br/>
[ERROR]         **Note**: To  use the 'Try it out' buttons, you must be logged in.
[ERROR]   version: 1.0.0

In this case, the YAML parser is failing because de description string is unquoted and has : characters.

The problem is that only the exception of the last parser is shown.
The relevante code is in SwaggerMojo.java:

            // iterate through mappers and see if one is able to parse
            for (BiFunction<String, Class<T>, T> mapper : mappers) {
                try {
                    instance = mapper.apply(fileContent, outputClass);
                    break;
                } catch (Exception e) {
                    caughtEx = e;
                }
            }

            // if no mapper could read the content correctly, finish with error
            if (instance == null) {
                if (caughtEx == null) {
                    caughtEx = new IllegalStateException("undefined state");
                }
                getLog().error(format("Could not read file '%s' for config %s", pathObj.toString(), configName), caughtEx);
                throw new IllegalStateException(caughtEx.getMessage(), caughtEx);
            }

I would be happy to contribute a PR, but I am not sure what the preferred solution would be:

  1. Separate the parsers by file extension, instead of only sorting them
  2. Keep all the exceptions and:
    2.1. log them all
    2.2 try to log only the one corresponding to the preferred parser
@frantuma
Copy link
Member

@adagios thanks for reporting and looking into this! I would probably go for 2.1 with separate logs including the mapper "kind" (preferred or not..) , if not 2.2 would be also better than current

adagios added a commit to adagios/swagger-core that referenced this issue Apr 20, 2021
adagios added a commit to adagios/swagger-core that referenced this issue Apr 20, 2021
@oliverlockwood
Copy link

I just hit this. It was a pain as I spent 20 mins trying to figure out why it was trying to read my YML file as JSON.

@frantuma
Copy link
Member

frantuma commented Nov 2, 2022

fixed in #3940

@frantuma frantuma closed this as completed Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants