OpenAPITools / OpenAPITools/openapi-generator
[BUG] Nullable and blank allowed properties result in incorrectly generated enum check
Nobody has claimed this yet.
- 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?
- 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
When using a generated DRF Spectacular schema which includes properties that are both nullable and can be blank, the Python generated client has an incorrect <enum-name>_validate_enum method in the object model class.
The method is:
@field_validator('my_type')
def my_type_validate_enum(cls, value):
"""Validates the enum"""
if value is None:
return value
if value not in (0, 1, 2, 3, 4, 5, 6, null):
raise ValueError("must be one of enum values (0, 1, 2, 3, 4, 5, 6, null)")
return value
However, the null value is not known. I think this should be parsed to None by the generator.
openapi-generator version
7.1.0 - Python
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: My Example API
version: 1.0.0
paths:
/objects/:
post:
operationId: objects_create
tags:
- objects
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/MyObject'
responses:
'201':
content:
application/json:
schema:
$ref: '#/components/schemas/MyObject'
description: ''
components:
schemas:
MyObject:
type: object
properties:
myType:
enum:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- null
type: integer
nullable: true
minimum: 0
maximum: 2147483647
Generation Details
Nothing special, I just use the docker image with options: `generate -i schema.yaml -g python -o openapi-generator/python/ -c config.json
Steps to reproduce
Generate any schema with a - null option in the enum list, this will be added as is to the if statement in the Python client.
Related issues/PRs
I could not find any.
Suggest a fix
The null option should be converted to the known Python None value. My guess would be that the issue is here: https://github.com/OpenAPITools/openapi-generator/blob/dd4143b75e58aaea4ba8a3d6b598386b550ffda8/modules/openapi-generator/src/main/resources/python/model_generic.mustache#L55-L81
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/python/model_generic.mustache around lines 55-81, then generate a Python client from the YAML schema in the issue using the Python generator. Check the generated enum validator for the nullable enum value. Done means the generated validation code uses Python's known None value rather than null and still handles the listed numeric values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100