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

NullPointerException in StaticHtmlGenerator when components: doesn't have schema: #1623

Closed
jmalin-splunk opened this issue Dec 5, 2018 · 4 comments

Comments

@jmalin-splunk
Copy link

jmalin-splunk commented Dec 5, 2018

Description

I found a possible bug the 3.0.3 generator for html. If a file has a components object that doesn't contain a schemas object, I get a NullPointerException in the StaticHtmlGenerator.

I've tested this against 17 different .yaml files. The only ones that fail have a components object that contains a securitySchemes object but not a schemas object.

The full error is:

Exception in thread "main" java.lang.NullPointerException
	at org.openapitools.codegen.languages.StaticHtmlGenerator.preprocessOpenAPI(StaticHtmlGenerator.java:197)
	at org.openapitools.codegen.DefaultGenerator.configureGeneratorProperties(DefaultGenerator.java:183)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:854)
	at org.openapitools.codegen.cmd.Generate.run(Generate.java:349)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:62)
openapi-generator version

v3.0.3. Don't know if it's a regression, because I haven't used an earlier version.

OpenAPI declaration file content or url

The entire contents of the components object is:

components:
  securitySchemes:
    SignalFxAuthenticationScheme:
      type: http
      description: >-
        Authentication with the SignalFx API using an **org** token (referred to
        as an **access** token in the web UI).
      scheme: bearer
      bearerFormat: SignalFx

Sample YAML file available as a Gist:

https://gist.github.com/jmalin-signalfx/0d18add1615f3fe19de5da716ad9b9ed

Command line used for generation
#!/usr/local/bin/bash
#
# OAS v3 yaml to HTML generator
#
# set -x
apidir="/Users/joemalin/src/api-reference"
outputdir="${apidir}/output/oas-html-v4"
generatedir="/Users/joemalin/src/openapi-generator-4/modules/openapi-generator-cli/target"
if [ -f "${outputdir}/${1}_api.html" ]; then 
    rm ${outputdir}/${1}_api.html
fi
java -jar ${generatedir}/openapi-generator-cli.jar generate \
--generator-name 'html' \
--input-spec ${apidir}/${1} \
--template-dir ${apidir}/templates/html-v4-new \
--verbose \
--output ${outputdir} > ${apidir}/logs/oas4-runlog-$(date --iso-8601='seconds').log
mv ${outputdir}/index.html ${outputdir}/${1}_api.html
Steps to reproduce

Attempt to generate HTML from the YAML.

Note: I don't think this is related to templates. I've customized the templates, but 15 YAML files work, and 2 dont', all using the same templates.

Related issues/PRs

Haven't found any.

Suggest a fix/enhancement

I looked at StaticHtmlGenerator.preprocessOpenAPI(StaticHtmlGenerator.java line 197. It's in the following method:

    public void preprocessOpenAPI(OpenAPI openAPI) {
        Info info = openAPI.getInfo();
        info.setDescription(toHtml(info.getDescription()));
        info.setTitle(toHtml(info.getTitle()));
        Map<String, Schema> models = openAPI.getComponents().getSchemas();
        for (Schema model : models.values()) {
            model.setDescription(toHtml(model.getDescription()));
            model.setTitle(toHtml(model.getTitle()));
        }
    }

I notice that the method seems to be getting Schemas objects from the Components objects, but it's not checking to see if a Schemas object actually exists. According to the OAS3 3.0.2 specification, none of the fields within the Components object is required, so a Components object that only contains a securitySchemes object is valid.

@wing328
Copy link
Member

wing328 commented Dec 6, 2018

v3.0.3. Don't know if it's a regression, because I haven't used an earlier version.

Can you please test with the latest stable version instead and if it's still an issue?

@jmini
Copy link
Member

jmini commented Dec 6, 2018

@wing328 : the code is still the same on master:

public void preprocessOpenAPI(OpenAPI openAPI) {
Info info = openAPI.getInfo();
info.setDescription(toHtml(info.getDescription()));
info.setTitle(toHtml(info.getTitle()));
Map<String, Schema> models = openAPI.getComponents().getSchemas();
for (Schema model : models.values()) {
model.setDescription(toHtml(model.getDescription()));
model.setTitle(toHtml(model.getTitle()));
}
}

Replacing line 197 with Map<String, Schema> models = ModelUtils.getSchemas(openAPI) should solve the NPE.

@jmalin-signalfx can you propose a pull request? do you need help for that?

@wing328
Copy link
Member

wing328 commented Dec 18, 2018

@jmalin-signalfx do you need help filing a fix? Let us know.

@wing328
Copy link
Member

wing328 commented Jan 20, 2019

@jmalin-signalfx The issue has been fixed by @jmini. Please pull the latest master to give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants