question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] python/python-experimental generator got NPE when handle object schema in query parameters

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
openapi-generator version

5.4.0

6.0.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: foo api
  version: v1
  description: 'bar'

paths:
  '/users':
    get:
      summary: user list
      operationId: userList
      parameters:
        - name: mapBean
          in: query
          required: false
          description: mapBean
          style: deepObject
          explode: true
          schema:
            type: object
            properties:
              keyword:
                title: keyword
                type: string
      responses:
        '200':
          description: ok
Generation Details

config:

projectName: foo-api
packageName: foo_api
Steps to reproduce
openapi-generator-cli generate \
  --generator-name python \
  --config foo-api-gen-config.yaml \
  --output foo-api \
  --input-spec foo-api.yaml

got error:

[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: python (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'python' is considered stable.
[main] INFO  o.o.c.languages.PythonClientCodegen - generateAliasAsModel is hard coded to true in this generator. Alias models will only be generated if they contain validations or enums
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.TemplateManager - writing file /Users/qianjia/tmp/oas-gen/foo-api/foo_api/model/map_bean.py
[main] INFO  o.o.codegen.TemplateManager - Skipped /Users/qianjia/tmp/oas-gen/foo-api/test/test_map_bean.py (Test files never overwrite an existing file of the same name.)
[main] INFO  o.o.codegen.TemplateManager - writing file /Users/qianjia/tmp/oas-gen/foo-api/docs/MapBean.md
[main] ERROR o.o.codegen.DefaultCodegen - Undefined property/schema for `map_bean`. Default to type:string.
[main] ERROR o.o.codegen.DefaultCodegen - Undefined property/schema for `map_bean`. Default to type:string.
Exception in thread "main" java.lang.RuntimeException: Could not process operation:
  Tag: class Tag {
    name: default
    description: null
    externalDocs: null
}
  Operation: userList
  Resource: get /users
  Schemas: {mapBean=class ObjectSchema {
    class Schema {
        type: object
        format: null
        $ref: null
        description: null
        title: null
        multipleOf: null
        maximum: null
        exclusiveMaximum: null
        minimum: null
        exclusiveMinimum: null
        maxLength: null
        minLength: null
        pattern: null
        maxItems: null
        minItems: null
        uniqueItems: null
        maxProperties: null
        minProperties: null
        required: null
        not: null
        properties: {keyword=class StringSchema {
            class Schema {
                type: string
                format: null
                $ref: null
                description: null
                title: keyword
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: null
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }}
        additionalProperties: null
        nullable: null
        readOnly: null
        writeOnly: null
        example: null
        externalDocs: null
        deprecated: null
        discriminator: null
        xml: null
    }
}}
  Exception: null
	at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1163)
	at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1054)
	at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:549)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:891)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.NullPointerException
	at org.openapitools.codegen.languages.PythonClientCodegen.fromProperty(PythonClientCodegen.java:440)
	at org.openapitools.codegen.DefaultCodegen.fromParameter(DefaultCodegen.java:4752)
	at org.openapitools.codegen.DefaultCodegen.fromOperation(DefaultCodegen.java:4113)
	at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1131)
	... 6 more
Related issues/PRs
Suggest a fix

PythonClientCodegen#fromProperty and PythonExperimentalClientCodegen#fromProperty doesn’t handle the situation that super.fromProperty may return null.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
spacethercommented, May 27, 2022

@nloding the model construction is similar between those generators. In python-experimental, spec case is used for all model property names though. Changes have not been moving from python-experimental into python because the python-experimental generator was re-written to more robustly handle schema validation. What is the definition of your object schema parameter? The style DeepObject is not yet supported in python-experimental.

Your options are:

  1. Try using python-experimental. and peg it to a specific version of openapi-generator. Then you will not get breaking changes unless you rev-roll.
  2. Submit a PR fixing this feature in the python generator
  3. Submit a PR adding deepObject serialization to python-experimental
  4. Modify your client/server to send this data in the request body rather than a parameter
0reactions
nlodingcommented, May 27, 2022

@spacether this is the parameter definition:

          {
            "name": "metadata",
            "in": "query",
            "schema": {
              "type": "object",
              "additionalProperties": {
                "type": "string",
                "nullable": true
              }
            },
            "style": "deepObject",
            "expand": true
          },

This seems to work in other generators, only python appears broken. I haven’t looked at how this code is generated at all, but I’ll look to see what I can find in the generator to fix it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to require at least one of two parameters in OpenAPI?
I'm using OpenAPI 3 and have two query parameters, at least one of which is required but it doesn ...
Read more >
Describing Parameters - Swagger
Each parameter has name , value type (for primitive value parameters) or schema (for request body), and optional description . Here is an...
Read more >
OpenAPI JSON Objects as Query Parameters - Baeldung
Learn how to specify JSON objects as query parameters using the OpenAPI specification.
Read more >
336529 – NULLPOINTEREXCEPTION GENERATING ... - Bugs
Generating a schema for the following class causes a NullPointer Exception: @javax.xml.bind.annotation.XmlAccessorType(value=FIELD) ...
Read more >
java.lang.NullPointerException - 2022
NullPointerException Example - How to handle Java Null Pointer Exception. ... before you request the method or a field from the object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found