OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.64k stars 6.29k forks source link

[BUG] python-fastapi generator problem with enumeration. #10127

Open vlmduy opened 2 years ago

vlmduy commented 2 years ago

Bug Report Checklist

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
DarioGonzalez-Axians commented 2 years ago

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

spacether commented 2 years ago

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:

  1. edit the templates in your fork of the repo
  2. post a PR of your changes
  3. run these instructions per our PR description steps:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
  4. commit the updated samples
  5. add any tests in the sample for fastapi here: https://github.com/OpenAPITools/openapi-generator/tree/master/samples/server/petstore/python-fastapi/tests
liulu1998 commented 1 year ago

I encountered the same issue, has it been fixed ?

spacether commented 1 year ago

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:

And here is a sample enum

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)
Holt59 commented 1 year ago

@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.

spacether commented 1 year ago

Yup they are for different use cases