swagger-api / swagger-api/swagger-codegen

[JAVA SPRING] Null pointer exception when specification doesn't have components

Open
#10,370 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

when the open API specification doesn't contain components then null pointer exception is thrown while generating models.
Error:
Exception in thread "main" java.lang.NullPointerException
at io.swagger.codegen.v3.DefaultGenerator.generateModels(DefaultGenerator.java:334)
at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:779)
at org.specocode.codegen.CodeGenerator.generateServerStub(CodeGenerator.java:73)
at org.specocode.codegen.CodeGenerator.main(CodeGenerator.java:207)

Swagger-codegen version

3.0.20

Swagger declaration file content or url
openapi: 3.0.1
info:
  title: Swagger Isuite
  description: '<p>This is a sample server Petstore server. You can find out more
    about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net,
    #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key`
    to test the authorization filters.</p>'
  version: 1.0.0
servers:
- url: https://api.pethub.com/pethub
- url: http://api.pethub.com/pethub
paths:
  /store:
    description: store details
    get:
      description: get all stores
      responses:
        200:
          description: store information details
          content:
            'application/json':
              schema:
                type: object
                properties:
                  id:
                    type: string
                  name:
                    type: string
                  location:
                    type: string
                  pincode:
                    type: number
Steps to reproduce
  1. Parse json and get OpenAPI object
  2. Config used for codegen is as below,
    CodegenConfig config = CodegenConfigLoader.forName("spring");
    ((SpringCodegen) config).setApiPackage("api");
    ((SpringCodegen) config).setArtifactId("org.mycode.pet");
    ((SpringCodegen) config).setBasePackage("org.mycode");
    ((SpringCodegen) config).setArtifactVersion("1.0.0");
    ((SpringCodegen) config).setModelPackage("model");
    ((SpringCodegen) config).setConfigPackage("config");
    ((SpringCodegen) config).setGroupId("org.specocode");
    ((SpringCodegen) config).setJava8(true);
    ((SpringCodegen) config).setLibrary("spring-boot");
    config.additionalProperties().put("basePackage", "org.specocode");
    config.setOutputDir("someOutputfolder");
  3. ClientOptInput is created
    ClientOptInput opts = new ClientOptInput();
    opts.setOpenAPI(openAPI);
    opts.setConfig(config);
  4. DefaultGenerator's generate method is called
    DefaultGenerator generator = new DefaultGenerator();
    List files = generator.opts(opts).generate();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in DefaultGenerator.java at generateModels line 334, then follow the generate call shown in the stack trace. Reproduce the failure with the supplied OpenAPI YAML, which has paths but no components. Done means generation completes without a NullPointerException for specifications without components, with regression coverage for that input.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.