Skip to content

[Any] Code generation fails when using parameter in another file #5106

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

Closed
rwf-hcl opened this issue Mar 17, 2017 · 2 comments
Closed

[Any] Code generation fails when using parameter in another file #5106

rwf-hcl opened this issue Mar 17, 2017 · 2 comments
Milestone

Comments

@rwf-hcl
Copy link

rwf-hcl commented Mar 17, 2017

Description

When referencing a parameter defined in another file, the swagger-editor successfully validates the YAML and creates the API documentation, but code generation fails with a NullPointer exception and no further details. If the same parameter is defined in the parameters section of the single file, then everything is ok.

Any code generation fails. I specifically tried server-JAX-RS and client-HTML.

Swagger-codegen version

swagger-editor 2.10.3
swagger-codegen 2.2.2

Swagger declaration file content or url

-- main yaml file ---------------------------

swagger: "2.0"

info:
  title: Test Case for Code Gen Issue - Fails
  description: |
    Test case to demonstrate the problem with generating code when external parameter references are used.
    This is the failure test case.
  version: 0.0.1

produces:
  - application/json

paths:

  /test/fails:

    get:
      parameters:
        - $ref: 'fails_param.yaml#/parameters/TheParam'
      responses:
        "200":
          $ref: '#/responses/TheResponse'

parameters: {} # are in fails_param.yaml


responses:

  TheResponse:
    description: The response.
    schema:
      $ref: '#/definitions/TheResponseSchema'

definitions:

  TheResponseSchema:
    description: The response schema.
    type: object
    properties:
      prop1:
        type: integer
      prop2:
        type: string

# end

-- included file (params) ------------------

# parameters used in fails_main.yaml

swagger: "2.0"

info:
  title: Parameters for Test Case for Code Gen Issue - Fails
  description: |
    Parameters for the test case to demonstrate the problem with generating 
    code when external parameter references are used.
    This is the failure test case.
  version: 0.0.1

parameters:

  TheParam:
    name: param
    in: query
    type: string
    description: The parameter.
# end

-- end YAML files --------------------------------

Command line used for generation

Initially done from swagger-editor (local)

java -jar C:\Utils\swagger-tools\swagger-codegen-cli.jar generate -l jaxrs -i fails_main.yaml

Steps to reproduce

Use above swagger-codegen-cli command in a directory with both the above YAML files.

Exception reported by swagger-codegen-cli
Exception in thread "main" java.lang.NullPointerException
        at io.swagger.codegen.DefaultGenerator.generateParameterId(DefaultGenerator.java:834)
        at io.swagger.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:758)
        at io.swagger.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:727)
        at io.swagger.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:356)
        at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:666)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:234)
        at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:41)

The problem seems to be that the $ref in the parameters section is not properly parsed. In the verbose mode, it shows a null parameter list.

  "paths" : {
    "/test/fails" : {
      "get" : {
        "parameters" : [ null ],
        "responses" : {
          "200" : {
            "description" : "The response.",
            "schema" : {
              "$ref" : "#/definitions/TheResponseSchema"
            }
          }
        }
      }
    }
  },
Related issues
Suggest a Fix
@rwf-hcl
Copy link
Author

rwf-hcl commented Mar 17, 2017

Update: When prefixing the referenced file with "./" codgen works when using the CLI. But it still fails from the editor (same way).
It should not be necessary to use the "./" prefix, and it works without the prefix for definitions.

@wing328 wing328 added this to the v2.2.3 milestone Apr 27, 2017
@wing328
Copy link
Contributor

wing328 commented Apr 27, 2017

@RWFischer The behaviour is determined by Swagger Parser and here is the PR to enhance it: swagger-api/swagger-parser#422

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

2 participants