openapi-generator
openapi-generator copied to clipboard
[BUG] python-fastapi generator problem with enumeration.
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] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
openapi-generator-cli -g python-fastapi generator cannot generate the valid enum class.
openapi-generator version
5.2.0
OpenAPI declaration file content or url
components:
schemas:
example.test_enumeration:
type: string
title: This is Enum
enum:
- ENUM_1
- ENUM_2
- ENUM_3
Generation Details
# coding: utf-8
from __future__ import annotations
from datetime import date, datetime # noqa: F401
import re # noqa: F401
from typing import Any, Dict, List, Optional # noqa: F401
from pydantic import AnyUrl, BaseModel, EmailStr, validator # noqa: F401
class ExampleTestEnumeration(BaseModel):
"""NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
Do not edit the class manually.
ExampleTestEnumeration - a model defined in OpenAPI
"""
ExampleTestEnumeration.update_forward_refs()
Steps to reproduce
Related issues/PRs
Hi! We would like to contribute and solve this issue. I think we can easily solve this and upload a PR, but we need guidance on how the generator code and the *.mustache
templates work. Can someone explain this real quick for us?
Thanks in advance, Axians Spain Development Team
Here are the docs on our mustache template library: https://github.com/spullara/mustache.java Here are some docs on how mustache is used: http://mustache.github.io/mustache.5.html Please read through the FastAPI generator here: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFastAPIServerCodegen.java#L130 It processes the openapi spec file in Java and renders the mustache templates into python code. The PythonFastAPIServerCodegen templates are here: https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/python-fastapi
In order to contribute you would:
- edit the templates in your fork of the repo
- post a PR of your changes
- run these instructions per our PR description steps:
./mvnw clean package
./bin/generate-samples.sh
./bin/utils/export_docs_generators.sh
- commit the updated samples
- add any tests in the sample for fastapi here: https://github.com/OpenAPITools/openapi-generator/tree/master/samples/server/petstore/python-fastapi/tests
I encountered the same issue, has it been fixed ?
Nope. Enums are fully working in python-experimental
though, how about trying it?
Or would you like to file a PR fixing python-fastapi
?
One can see enums working in python-experimental
in these references:
- spec is here, which includes an enum tag
- enum api docs
- tests of enum serialization + deserialization
- tests of enum model instantiation
- all tests are passing per node4 tests
class EnumWith0DoesNotMatchFalse(
_SchemaEnumMaker(
enum_value_to_name={
0: "POSITIVE_0",
}
),
NumberSchema
):
"""NOTE: This class is auto generated by OpenAPI Generator.
Ref: https://openapi-generator.tech
Do not edit the class manually.
"""
@classmethod
@property
def POSITIVE_0(cls):
return cls(0)
@spacether python
(prior python-experimental
) is a client generator while python-fastapi
is a server generator, so it's complicated to use one in place of the other.
Yup they are for different use cases