openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][All languages] NullPointerException on nested ComposedSchema

Open DonDi94 opened this issue 7 years ago • 13 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] Have you search for related issues/PRs?
  • [ ] What's the actual output vs expected output?
Description

I tried to generate code from a yaml with the latest commit of the generator, but I got the following exception:

Exception in thread "main" java.lang.RuntimeException: Could not process model 'ComplexData'.Please make sure that your schema is correct! at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:458) at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:899) at org.openapitools.codegen.cmd.Generate.run(Generate.java:368) at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:60) Caused by: java.lang.NullPointerException at org.openapitools.codegen.DefaultCodegen.addProperties(DefaultCodegen.java:1831) at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:1737) at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1137) at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:453) ... 3 more

I tried to get into the source and it seems to be a problem with ComposedSchema handling. At line 1831 in addProperties the allOf is processed so that all of the properties defined in it are added to required and properties (lines 1852 and 1855). There is a check for the input variable schema to be an instance of ComposedSchema, but that is true for oneOf, anyOf and allOf, not just allOf. If we have a allOf all of its properties will be processed by the for loop recursively, if one of its properties is a oneOf it will be passed recursively to addProperties, the condition schema instanceOf ComposedSchema will be true and the procedure will continue to the for loop. As we are handling a oneOf, schema.getAllOf() will return null and create a NullPointerException.

openapi-generator version

4.0.0, commit b5ede4b

OpenAPI declaration file content or url
openapi: '3.0.0'
info:
  version: 1.0.0
  title: ComplexTest
  description: A sample API that uses a complex schema
paths: 
  /test:    
    post:
      operationId: ComplexTest
      requestBody:
        content:
          'multipart/form-data+json':
            schema:
              $ref: '#/components/schemas/ComplexData'
      responses:
        default:
          $ref: '#/components/responses/DefaultResponse'
components:
  schemas:
    ComplexData:
      type: object
      properties:
        username:
          type: string
        email:
          type: string
      allOf:
        - oneOf:
          - required: [ username ]
          - required: [ email ]
  responses:
    DefaultResponse:
      description: unexpected error
      content:       
        application/json:
          schema:
            type: object
            properties:
              message:
                type: string
Command line used for generation

java -jar openapi-generator-cli.jar generate -i complexdata.yaml -g erlang-client -o client/

java -jar openapi-generator-cli.jar generate -i complexdata.yaml -g erlang-server -o client/

Steps to reproduce
  • Create a file complexdata.yaml with the given yaml specifications
  • Generate the code in any language
Related issues/PRs

I haven't found related issues other than old oneOf issues.

DonDi94 avatar Apr 05 '19 12:04 DonDi94

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

auto-labeler avatar Apr 05 '19 12:04 auto-labeler

  schemas:
    ComplexData:
      type: object
      properties:
        username:
          type: string
        email:
          type: string
      allOf:
        - oneOf:
          - required: [ username ]
          - required: [ email ]

What does the payload look like?

I think we may be able to find a way to model the payload differently to workaround the issue.

wing328 avatar Jun 20 '19 06:06 wing328

I'm not sure whether I understand the question. By payload you mean the value that I would send to represent the schema ComplexData, right?

In that case, I haven't modeled this example from my own data. Instead, I found this issue while trying to generate code from the 3GPP 5G Core specifications. You can find this kind of nested composed schemas quite frequently, such as ServiceAreaRestriction in TS29571_CommonData.yaml. (http://www.3gpp.org/ftp/Specs/archive/29_series/29.571/)

ServiceAreaRestriction:
  type: object
  properties:
    restrictionType:
      $ref: '#/components/schemas/RestrictionType'
    areas:
      type: array
      items:
        $ref: '#/components/schemas/Area'
    maxNumOfTAs:
      $ref: '#/components/schemas/Uinteger'
  allOf:
    - oneOf:
      - not:
        required:
          - restrictionType
      - required:
          - areas
    - anyOf:
      - not:
        required:
          - restrictionType
        properties:
          restrictionType:
            type: string
            enum:
              - NOT_ALLOWED_AREAS
      - not:
        required:
          - maxNumOfTAs

As I was more interested into the parsed JSON rather than the mustache-ready processed value, I simply added a guard for the "getAllOf" and removed the exceptions. That was enough for what I was interested in doing (I am generating for erlang-server), but that limits the generation for erlang-client and probably all the other languages that would create models/data structures with it.

protected void addProperties(Map<String, Schema> properties, List<String> required, Schema schema) {
    if (schema instanceof ComposedSchema) {
        ComposedSchema composedSchema = (ComposedSchema) schema;

        if (composedSchema.getAllOf() != null) {
            for (Schema component : composedSchema.getAllOf()) {
                addProperties(properties, required, component);
            }
        }

        if (composedSchema.getOneOf() != null) {
            //throw new RuntimeException("Please report the issue: Cannot process oneOf (Composed Scheme) in addProperties: " + schema);
            System.out.println("GetOneOf should be null, but it is not");
        }

        if (composedSchema.getAnyOf() != null) {
            //throw new RuntimeException("Please report the issue: Cannot process anyOf (Composed Schema) in addProperties: " + schema);
            System.out.println("GetAnyOf should be null, but it is not");
        }

        return;
    }
    //...
}

DonDi94 avatar Jun 20 '19 07:06 DonDi94

Thanks for opening this. I am also facing similar issues, especially while working with the 5G Core API's hosted at https://github.com/jdegre/5GC_APIs.

Tried multiple options including generating from https://editor.swagger.io/

Exception in thread "Thread-1" java.lang.RuntimeException: Could not process model 'PatchOperation'.Please make sure that your schema is correct! at io.swagger.codegen.v3.DefaultGenerator.generateModels(DefaultGenerator.java:378) at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:756) at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:342) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.NullPointerException at io.swagger.codegen.v3.generators.util.OpenAPIUtil.getSimpleRef(OpenAPIUtil.java:32) at io.swagger.codegen.v3.generators.SchemaHandler.configureAnyOfModel(SchemaHandler.java:75) at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromModel(DefaultCodegenConfig.java:1374) at io.swagger.codegen.v3.generators.java.AbstractJavaCodegen.fromModel(AbstractJavaCodegen.java:895) at io.swagger.codegen.v3.DefaultGenerator.processModels(DefaultGenerator.java:988) at io.swagger.codegen.v3.DefaultGenerator.generateModels(DefaultGenerator.java:349)

shameerkc avatar Jul 08 '19 08:07 shameerkc

I am also facing the same issue with latest https://github.com/jdegre/5GC_APIs.

[main] INFO o.o.c.l.CppPistacheServerCodegen - Using get/set-based model template Exception in thread "main" java.lang.RuntimeException: Could not process model 'ServiceAreaRestriction'.Please make sure that your schema is correct! at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:472) at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:923) at org.openapitools.codegen.cmd.Generate.run(Generate.java:416) at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:60) Caused by: java.lang.NullPointerException at org.openapitools.codegen.DefaultCodegen.addProperties(DefaultCodegen.java:2001) at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:1888) at org.openapitools.codegen.languages.CppPistacheServerCodegen.fromModel(CppPistacheServerCodegen.java:183) at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1200) at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:467) ... 3 more

amitinfo2k avatar Oct 09 '19 11:10 amitinfo2k

Labels with Erlang are not correct, but title.

Run in the same issue with TypeScript. Use Case: Definition of some sort of MongoDB Query, which contains $and and $or.

Bessonov avatar Oct 24 '19 13:10 Bessonov

Getting the same error while trying to generate an HTML representation of the API.

MiguelAraCo avatar Dec 19 '19 06:12 MiguelAraCo

Same error as @DonDi94.

Edit: Using jaxrs-jersey

jpestima avatar Feb 27 '20 14:02 jpestima

Same error for typescript-angular

eachirei avatar Feb 28 '20 08:02 eachirei

typescript-axios also affected, guess this is a general problem.

simllll avatar Apr 03 '20 19:04 simllll

I can reproduce this, with a pretty simple example as below. I believe I'm doing the inheritance correctly (although the docs are quite unclear around some parts), but even if I'm not it probably shouldn't fail with an NPE.

openapi: 3.0.0
info:
  version: 1.0.0
  title: reproducer

paths:
  /foo:
    get:
      responses:
        '500':
          description: error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GeneralError'

components:
  schemas:
    GeneralError:
      anyOf:
        - $ref: '#/components/schemas/SpecificError'
      discriminator:
        propertyName: code
        mapping:
          SPECIFIC_ERROR: '#/components/schemas/SpecificError'

    SpecificError:
      allOf:
        - $ref: '#/components/schemas/GeneralError'
        - type: object
          properties:
            detail:
              type: string
          required:
            - detail

doctau avatar Apr 09 '20 05:04 doctau

If you don't have anyOf/oneOf in General error then it does not fail during generation, but nothing is generated for SpecificError (unless there is another reference to it).

doctau avatar Jun 09 '20 04:06 doctau

no longer seeing the issue in the latest stable version v7.0.0

please open a new issue instead if you still need help on this.

wing328 avatar Sep 04 '23 15:09 wing328