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

NPE when responses are referenced from external yaml files #1155

Closed
srividhyak27 opened this issue Oct 1, 2018 · 11 comments
Closed

NPE when responses are referenced from external yaml files #1155

srividhyak27 opened this issue Oct 1, 2018 · 11 comments

Comments

@srividhyak27
Copy link

srividhyak27 commented Oct 1, 2018

Description

Exception in code-gen when responses are referenced from external yaml files

java.lang.NullPointerException
	at io.swagger.v3.parser.ResolverCache.updateLocalRefs(ResolverCache.java:162)
	at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:152)
	at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalResponse(ExternalRefProcessor.java:205)
	at io.swagger.v3.parser.processors.ResponseProcessor.processReferenceResponse(ResponseProcessor.java:76)
	at io.swagger.v3.parser.processors.ResponseProcessor.processResponse(ResponseProcessor.java:38)
	at io.swagger.v3.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:56)
	at io.swagger.v3.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:83)
	at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:49)
	at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:53)
	at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:19)
openapi-generator version
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>3.3.0-SNAPSHOT</version>

Same issue with 3.2.3 version as well.

OpenAPI declaration file content or url
paths:
  /{appId}/subscriptions:
    get:
      summary: read all of the active subscriptions for the app
      
      responses:
        '200':
          description: OK (Successful)
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/subscription'
        '400':
          $ref: './common.yaml#/components/responses/E400'
        '401':
          $ref: './common.yaml#/components/responses/E401'

In common.yaml, responses are defined

openapi: 3.0.0
info:
  title: Common Data Types
  version: "1.0"
paths: {}
components:
  schemas:
    ProblemDetails:
      type: object
      properties:
        title:
          type: string
          description: A short, human-readable summary of the problem
        status:
          type: integer
          description: The HTTP status code for this occurrence of the problem.
  responses:
    E400:
      description: Bad request
      content:
        application/problem+json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
    E401:
      description: Unauthorized
      content:
        application/problem+json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
Command line used for generation

No

Steps to reproduce

mvn generate-sources

@wing328
Copy link
Member

wing328 commented Oct 2, 2018

Please provide the spec so that the community can more easily repeat the issue and help you out.

@srividhyak27
Copy link
Author

srividhyak27 commented Oct 3, 2018

Updated the issue with yaml content.

When I remove the references to responses in the main yaml file, code-generation works fine
Thanks,
Srividhya

@ackintosh
Copy link
Contributor

👀

@ackintosh
Copy link
Contributor

@srividhyak27 Thanks for the issue!
I've tested code generation with the YAML files below (based on which you reported).
But I couldn't reproduce the issue. Something wrongs my steps to reproduce?


openapi.yaml

openapi: 3.0.0
info:
  title: Common Data Types
  version: "1.0"
paths:
  /{appId}/subscriptions:
    get:
      summary: read all of the active subscriptions for the app
      
      responses:
        '200':
          description: OK (Successful)
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/subscription'
        '400':
          $ref: './common.yaml#/components/responses/E400'
        '401':
          $ref: './common.yaml#/components/responses/E401'
components:
  schemas:
    subscription:
      type: string

common.yaml

openapi: 3.0.0
info:
  title: Common Data Types
  version: "1.0"
paths: {}
components:
  schemas:
    ProblemDetails:
      type: object
      properties:
        title:
          type: string
          description: A short, human-readable summary of the problem
        status:
          type: integer
          description: The HTTP status code for this occurrence of the problem.
  responses:
    E400:
      description: Bad request
      content:
        application/problem+json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
    E401:
      description: Unauthorized
      content:
        application/problem+json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'

$ tree
.
├── common.yaml
└── openapi.yaml
$ openapi-generator version
3.3.0
$ openapi-generator generate -g php -o /tmp/issue1155 -i openapi.yaml
[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - No .openapi-generator-ignore file found.
[main] WARN  o.o.codegen.utils.URLPathUtils - 'host' not defined in the spec (2.0). Default to [http://localhost] for server URL [http://localhost/]
[main] WARN  o.o.codegen.utils.URLPathUtils - 'host' not defined in the spec (2.0). Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/lib/Model/ProblemDetails.php
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/test/Model/ProblemDetailsTest.php
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/docs/Model/ProblemDetails.md
[main] WARN  o.o.codegen.DefaultCodegen - Empty operationId found for path: get /{appId}/subscriptions. Renamed to auto-generated operationId: appIdSubscriptionsGet
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/lib/Api/DefaultApi.php
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/test/Api/DefaultApiTest.php
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/docs/Api/DefaultApi.md
[main] WARN  o.o.codegen.utils.URLPathUtils - 'host' not defined in the spec (2.0). Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/lib/ApiException.php
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/lib/Configuration.php
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/lib/ObjectSerializer.php
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/lib/Model/ModelInterface.php
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/lib/HeaderSelector.php
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/composer.json
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/README.md
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/phpunit.xml.dist
[main] INFO  o.o.codegen.DefaultGenerator - writing file /tmp/issue1155/.travis.yml
[main] INFO  o.o.codegen.DefaultGenerator - writing file /tmp/issue1155/.php_cs
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/git_push.sh
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/.openapi-generator-ignore
[main] INFO  o.o.codegen.AbstractGenerator - writing file /tmp/issue1155/.openapi-generator/VERSION
$

@bilak
Copy link
Contributor

bilak commented Oct 25, 2018

I have the same problem in my project. Unfortunatelly I can't provide the code. But if I can help somehow with debug let me know.

@bilak
Copy link
Contributor

bilak commented Oct 25, 2018

Ok so problem is with response like this:

    E401:
      description: Unauthorized
      headers:
        WWW-Authenticate:
          description: Contains base of URL for creation of authentication request
          schema:
            type: string

edit:
I fixed it with

    E401:
      description: Unauthorized
      headers:
        WWW-Authenticate:
          description: Contains base of URL for creation of authentication request
          schema:
            type: string
      content:
        '*/*':
          schema:
            type: null

this seems to be working for class generation, hovewer it's not valid

@its-atique1987
Copy link

its-atique1987 commented Apr 16, 2019

Is there any update on this as me too facing similar issue.

If the response tag in common.yaml is just below as mention , the issue still persists. Not sure why. Any Help?

responses:
E400:
description: Bad request
E401:
description: Unauthorized

@its-atique1987
Copy link

One observation is that, if 'schemas' is referred from external file insted of 'responses', the code generation works.
So ! What's the issue if 'responses' being referred from external yaml file?

BELOW WORKS - 'schemas' referred externally instead of 'response'

openapi.yaml

openapi: 3.0.0
info:
  title: Common Data Types
  version: "1.0"
paths:
  /{appId}/subscriptions:
    get:
      summary: read all of the active subscriptions for the app
      
      responses:
        '200':
          description: OK (Successful)
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: './common.yam#/components/schemas/subscription'
        '400':
          $ref: '#/components/responses/E400'
        '401':
          $ref: '#/components/responses/E401'
components:
 responses:
    E400:
      description: Bad request
    E401:
      description: Unauthorized

common.yaml
openapi: 3.0.0
info:
  title: Common Data Types
  version: "1.0"
paths: {}
components:
  schemas:
    subscription:
      type: string

$ tree
.
├── common.yaml
└── openapi.yaml
$ openapi-generator version
3.3.0

@ackintosh
Copy link
Contributor

Sorry for the late reply. The issue has fixed in latest version.

Please give it a try with v4.0.0-beta3. 😉

@ackintosh
Copy link
Contributor

Please reopen this or report a new issue if still experiencing the issue. Thank you!

@its-atique1987
Copy link

its-atique1987 commented Apr 19, 2019

@ackintosh Thanks for response. Yes the version v4.0.0-beta3 solved the issue. :)

Also, please update me on the following issue .
#2701

Cheers,

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

5 participants