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 with composed-schema containing oneOf in the components #15

Closed
jmini opened this issue Feb 19, 2018 · 7 comments
Closed
Assignees

Comments

@jmini
Copy link
Contributor

jmini commented Feb 19, 2018

I have some trouble with a OpenAPI spec containing a oneOf switch. (from documentation Data Models (Schemas) > oneOf, anyOf, allOf, not)

When I try to generate the java client, I get an error caused by a null pointer Exception (version 1.0.0-SNAPSHOT).

OpenAPI Spec:

openapi: 3.0.1
info:
  title: oneOf test
  version: '1.0'
servers:
  - url: 'http:https://localhost:8000/'
paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        '200':
          description: Updated
components:
  schemas:
    Pet:
      oneOf:
        - $ref: '#/components/schemas/Cat'
        - $ref: '#/components/schemas/Dog'
    Dog:
      type: object
      properties:
        bark:
          type: boolean
        breed:
          type: string
          enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:
      type: object
      properties:
        hunts:
          type: boolean
        age:
          type: integer

Stacktrace:

Exception in thread "main" java.lang.RuntimeException: Could not process model 'Pet'.Please make sure that your schema is correct!
	at io.swagger.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:361)
	at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:726)
	at <my code>
Caused by: java.lang.NullPointerException
	at io.swagger.codegen.languages.DefaultCodegenConfig.fromModel(DefaultCodegenConfig.java:1210)
	at io.swagger.codegen.languages.java.AbstractJavaCodegen.fromModel(AbstractJavaCodegen.java:845)
	at io.swagger.codegen.DefaultGenerator.processModels(DefaultGenerator.java:959)
	at io.swagger.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:356)
	... 3 more

Note:

When instead of having a Pet schema in the components, the composed-schema is inlined in the requestBody (as in the example from the docs), then the generator works.

openapi: 3.0.1
info:
  title: oneOf test
  version: '1.0'
servers:
  - url: 'http:https://localhost:8000/'
paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
      responses:
        '200':
          description: Updated
components:
  schemas:
    Dog:
      type: object
      properties:
        bark:
          type: boolean
        breed:
          type: string
          enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:
      type: object
      properties:
        hunts:
          type: boolean
        age:
          type: integer
@jmini
Copy link
Contributor Author

jmini commented Feb 20, 2018

The first step is to fix the NullPointerException.

Should it be fixed in DefaultCodegenConfig (line 1210) by assuming the composed.getAllOf() can be null.
Or should getAllOf() in ComposedSchema (line 38) be null safe (in this case the change should be made in the swagger-core repository).

@HugoMario: have you an input for this question?

jmini added a commit to jmini/swagger-codegen-generators that referenced this issue Feb 21, 2018
@jmini
Copy link
Contributor Author

jmini commented Feb 21, 2018

I have tried the second solution (fixing ComposedSchema directly in the core), but from a JSON point of view, null and empty list are not the same:

Current JSON:

type: object
anyOf:
- $ref: '#/components/schemas/UserObject'
- $ref: '#/components/schemas/EmployeeObject'

JSON after my "null-safe" modification in swagger-core:

type: object
allOf: []
anyOf:
- $ref: '#/components/schemas/UserObject'
- $ref: '#/components/schemas/EmployeeObject'
oneOf: []

This is why I have considered that it is better to fix the NullPointerException in this repository => Pull Request #16

@HugoMario
Copy link
Contributor

agree

HugoMario added a commit that referenced this issue Feb 21, 2018
Issue #15: fix NullPointerException with ComposedSchema
@jmini
Copy link
Contributor Author

jmini commented Feb 21, 2018

Thank you!

@jmini jmini closed this as completed Feb 21, 2018
@srividhyak27
Copy link

Hi,
Is this fix available in 3.0.0-rc0 ?
I am having a similar issue where NullPointer exception is thrown, my schema uses "anyOf" .
Thanks,
Sri

@HugoMario
Copy link
Contributor

hey @srividhyak27 , i think this fix is available for 3.0.0-rc1, can you please check and let me know?

@srividhyak27
Copy link

srividhyak27 commented Aug 8, 2018

Hi @HugoMario , thanks,
this issue (NullPointerException when the schema has anyOf) seems to be fixed in 3.0.0-rc1.

But, in 3.0.0-rc1, below exception is thrown

Caused by: java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 3
v2\Java
   ^ 
       at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.escape(Pattern.java:2471)

Is it related to the issue mentioned here - swagger-api/swagger-codegen#8246

I am using windows and using swagger-codegen maven plugin to generate classes from the openapi 3.0 specification.
I also understand that the fix for this issue is not available in 3.0.0-rc1. Any suggestions for now ?

Thanks,
Srividhya

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

3 participants