OpenAPITools / OpenAPITools/openapi-generator

[bug] NullPointerException when I attempt to generate a Python API client

Open
#20,348 13 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?

I don't want to share the full spec as this is used internally and is quite long/would take a while to anonymize.

  • Have you validated the input using an OpenAPI validator?

I validated the swagger file with both the swagger online editor and the openapi-generator validator: openapi-generator validate -i redocly.swagger.yaml

Description

I'm getting a NullPointerException when I attempt to generate a Python API client:

Exception in thread "main" java.lang.NullPointerException: Cannot read field "isArray" because "cp" is null
	at org.openapitools.codegen.languages.AbstractPythonCodegen.getModelNameFromDataType(AbstractPythonCodegen.java:1176)
	at org.openapitools.codegen.languages.AbstractPythonCodegen.getModelNameFromDataType(AbstractPythonCodegen.java:1179)
	at org.openapitools.codegen.languages.AbstractPythonCodegen.createImportMapOfSet(AbstractPythonCodegen.java:1159)
	at org.openapitools.codegen.languages.AbstractPythonCodegen.postProcessAllModels(AbstractPythonCodegen.java:858)
	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:536)
	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:453)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:1303)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:535)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)

I can generate a client with other paid tools (speakeasy), but I'd prefer to use the free and open source tooling available here.

openapi-generator version

7.11.0-SNAPSHOT

OpenAPI declaration file content or url

https://gist.github.com/joseph-grosso/436781d1e73d66f77bb6c0a2933885af

Generation Details

Steps:

  • combined openapi yaml files into file linked above
  • ran generation command: openapi-generator generate -i redocly.swagger.yaml -g python -o ../python-api
Suggest a fix

The error returned:

java.lang.NullPointerException: Cannot read field "isArray" because "cp" is null
	at org.openapitools.codegen.languages.AbstractPythonCodegen.getModelNameFromDataType(AbstractPythonCodegen.java:1176)

Hopefully if the null was handled differently in this step then this could pass.

The full error output

I believe that the issue is related to one of the warnings below:

  • "allOf schema null containing multiple types (not model) is not supported at the moment."
  • Failed to get the schema name: null
  • WARN o.o.codegen.DefaultCodegen - Unknown type found in the schema: object. To map it, please use the schema mapping option (e.g. --schema-mappings in CLI)
  • INFO o.o.codegen.DefaultGenerator - Model not generated since it's an alias to map (without property) and generateAliasAsModel is set to false (default)
openapi-generator generate -i redocly.swagger.yaml -g python -o ../python-api

[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - Output directory (/Users/josephgrosso/github/Carbon-Hub-Backend/openapi/../python-api) does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
[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.l.AbstractPythonCodegen - Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE="/usr/local/bin/yapf -i"' (Linux/Mac)
[main] INFO  o.o.c.l.AbstractPythonCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: null
[main] WARN  o.o.codegen.InlineModelResolver - allOf schema `null` containing multiple types (not model) is not supported at the moment.
[main] INFO  o.o.codegen.InlineModelResolver - Inline schema created as search_activities_request. To have complete control of the model name, set the `title` field or use the modelNameMapping option (e.g. --model-name-mappings search_activities_request=NewModel,ModelA=NewModelA in CLI) or inlineSchemaNameMapping option (--inline-schema-name-mappings search_activities_request=NewModel,ModelA=NewModelA in CLI).
[main] WARN  o.o.codegen.InlineModelResolver - allOf schema `null` containing multiple types (not model) is not supported at the moment.
[main] INFO  o.o.codegen.InlineModelResolver - Inline schema created as add_asset_to_asset_group_request. To have complete control of the model name, set the `title` field or use the modelNameMapping option (e.g. --model-name-mappings add_asset_to_asset_group_request=NewModel,ModelA=NewModelA in CLI) or inlineSchemaNameMapping option (--inline-schema-name-mappings add_asset_to_asset_group_request=NewModel,ModelA=NewModelA in CLI).
[main] WARN  o.o.codegen.InlineModelResolver - allOf schema `null` containing multiple types (not model) is not supported at the moment.
[main] INFO  o.o.codegen.InlineModelResolver - Inline schema created as search_asset_groups_request. To have complete control of the model name, set the `title` field or use the modelNameMapping option (e.g. --model-name-mappings search_asset_groups_request=NewModel,ModelA=NewModelA in CLI) or inlineSchemaNameMapping option (--inline-schema-name-mappings search_asset_groups_request=NewModel,ModelA=NewModelA in CLI).
[main] WARN  o.o.codegen.InlineModelResolver - allOf schema `null` containing multiple types (not model) is not supported at the moment.
[main] INFO  o.o.codegen.InlineModelResolver - Inline schema created as calculator_results_request. To have complete control of the model name, set the `title` field or use the modelNameMapping option (e.g. --model-name-mappings calculator_results_request=NewModel,ModelA=NewModelA in CLI) or inlineSchemaNameMapping option (--inline-schema-name-mappings calculator_results_request=NewModel,ModelA=NewModelA in CLI).
[main] WARN  o.o.codegen.InlineModelResolver - allOf schema `null` containing multiple types (not model) is not supported at the moment.
[main] INFO  o.o.codegen.InlineModelResolver - Inline schema created as export_calculator_results_request. To have complete control of the model name, set the `title` field or use the modelNameMapping option (e.g. --model-name-mappings export_calculator_results_request=NewModel,ModelA=NewModelA in CLI) or inlineSchemaNameMapping option (--inline-schema-name-mappings export_calculator_results_request=NewModel,ModelA=NewModelA in CLI).
[main] WARN  o.o.codegen.InlineModelResolver - allOf schema `null` containing multiple types (not model) is not supported at the moment.
[main] INFO  o.o.codegen.InlineModelResolver - Inline schema created as search_company_unit_config_request. To have complete control of the model name, set the `title` field or use the modelNameMapping option (e.g. --model-name-mappings search_company_unit_config_request=NewModel,ModelA=NewModelA in CLI) or inlineSchemaNameMapping option (--inline-schema-name-mappings search_company_unit_config_request=NewModel,ModelA=NewModelA in CLI).
[main] WARN  o.o.codegen.InlineModelResolver - allOf schema `null` containing multiple types (not model) is not supported at the moment.
[main] WARN  o.o.codegen.InlineModelResolver - allOf schema `null` containing multiple types (not model) is not supported at the moment.
...
[main] INFO  o.o.codegen.DefaultGenerator - Model AggregatedCalculatorResults not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model AggregatedRecordValues not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model AnalyticsCalculatorInputs not generated since it's a free-form object
[main] INFO  o.o.codegen.DefaultGenerator - Model AnalyticsCalculatorAssetInputs not generated since it's a free-form object
[main] INFO  o.o.codegen.DefaultGenerator - Model InputRecordResults not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model AggregatedRecordValuesResults not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model CalculatorResultGroupByFields not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model CustomAttributeDataTypeList not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model CustomAttributeEntityTypeList not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model CustomAttributes not generated since it's a free-form object
[main] INFO  o.o.codegen.DefaultGenerator - Model DeviceAttribute not generated since it's a free-form object
[main] INFO  o.o.codegen.DefaultGenerator - Model DeviceTypeList not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model EstimationMethodDefaultRecordsList not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model CalculatorInputSources not generated since it's an alias to map (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model EquipmentTypeList not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model FacilityList not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
...
[main] INFO  o.o.codegen.DefaultGenerator - Model RecordFilterFields_allOf_flow_associated_facility_custom_attributes not generated since it's a free-form object
Exception in thread "main" java.lang.NullPointerException
        at org.openapitools.codegen.languages.AbstractPythonCodegen.getModelNameFromDataType(AbstractPythonCodegen.java:1176)
        at org.openapitools.codegen.languages.AbstractPythonCodegen.getModelNameFromDataType(AbstractPythonCodegen.java:1179)
        at org.openapitools.codegen.languages.AbstractPythonCodegen.createImportMapOfSet(AbstractPythonCodegen.java:1159)
        at org.openapitools.codegen.languages.AbstractPythonCodegen.postProcessAllModels(AbstractPythonCodegen.java:858)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:536)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:453)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:1303)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:535)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the linked OpenAPI declaration and the openapi-generator generate -i redocly.swagger.yaml -g python -o ../python-api command. Start in AbstractPythonCodegen.getModelNameFromDataType and follow the calls from createImportMapOfSet; done means the supplied declaration generates a Python client without the reported NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, python
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.