openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] python-fastapi generator problem with enumeration.

Open vlmduy opened this issue 3 years ago • 4 comments

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

vlmduy avatar Aug 11 '21 03:08 vlmduy

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

DarioGonzalez-Axians avatar Dec 01 '21 10:12 DarioGonzalez-Axians

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
  1. commit the updated samples
  2. add any tests in the sample for fastapi here: https://github.com/OpenAPITools/openapi-generator/tree/master/samples/server/petstore/python-fastapi/tests

spacether avatar Jan 11 '22 05:01 spacether

I encountered the same issue, has it been fixed ?

liulu1998 avatar Aug 03 '22 08:08 liulu1998

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)

spacether avatar Aug 03 '22 14:08 spacether

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

Holt59 avatar Sep 28 '22 15:09 Holt59

Yup they are for different use cases

spacether avatar Sep 28 '22 16:09 spacether