NullPointerException in StaticHtmlGenerator when components: doesn't have schema:
Created by: jmalin-splunk
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.