Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
Map<String, ModelsMap> result = super.postProcessAllModels(objs);
for (Map.Entry<String, ModelsMap> entry : result.entrySet()) {
for (ModelMap mo : entry.getValue().getModels()) {
ModelsMap models = entry.getValue();
postProcessModelsEnum(models);
for (ModelMap mo : models.getModels()) {
CodegenModel cm = mo.getModel();
// Add additional filename information for imports
mo.put("pyImports", toPyImports(cm, cm.imports));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ from __future__ import annotations
from datetime import date, datetime # noqa: F401

import re # noqa: F401
{{#models}}
{{#model}}
{{#isEnum}}
from enum import Enum # noqa: F401
{{/isEnum}}
{{/model}}
{{/models}}
from typing import Any, Dict, List, Optional # noqa: F401

from pydantic import AnyUrl, BaseModel, EmailStr, Field, validator # noqa: F401
Expand All @@ -18,6 +25,21 @@ from pydantic import AnyUrl, BaseModel, EmailStr, Field, validator # noqa: F401

{{#models}}
{{#model}}
{{#isEnum}}
class {{classname}}(str, Enum):

@spacether spacether Dec 12, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the enum is for schema integers, numbers, or boolean, or includes null?
This forces them all to inherit str

@Linus-Boehm Linus-Boehm Dec 19, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't enums in OpenApi not always string based?
https://swagger.io/docs/specification/data-models/enums/

The only way I found to specify integer like enums would be something like:

# Source: https://stackoverflow.com/questions/66465888/how-to-define-enum-mapping-in-openapi

Severity:
  type: integer
  oneOf:
    - title: HIGH
      const: 2
      description: An urgent problem
    - title: MEDIUM
      const: 1
    - title: LOW
      const: 0
      description: Can wait forever

But I think that would need anyways a different handling, as the example is not even typed as enum.

Even this being maybe not perfect, it is already a big improvement to no generation at all IMO.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enums can have any type. If type is unset enum values can be any json schema type

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide an example from openapi?

@spacether spacether Jul 28, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, here is an enum that contains a string, int, float, and boolean

EnumOfAnyType:
  enum:
  - "a"
  - 1
  - 3.14
  - true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what we're looking for?

ReportingType:
  type: integer
  enum:
    - 1
    - 7

"""NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

Do not edit the class manually.

"""
{{#allowableValues}}
{{#enumVars}}
{{name}} = {{{value}}}
{{/enumVars}}
{{/allowableValues}}

{{/isEnum}}
{{^isEnum}}
class {{classname}}(BaseModel):
"""NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

Expand Down Expand Up @@ -70,7 +92,15 @@ class {{classname}}(BaseModel):
return value
{{/pattern}}
{{/vars}}
{{/isEnum}}
{{/model}}
{{/models}}


{{#models}}
{{#model}}
{{^isEnum}}
{{classname}}.update_forward_refs()
{{/isEnum}}
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ class ApiResponse(BaseModel):
type: Optional[str] = Field(alias="type", default=None)
message: Optional[str] = Field(alias="message", default=None)


ApiResponse.update_forward_refs()
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ def name_pattern(cls, value):
assert value is not None and re.match(r"^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$", value)
return value


Category.update_forward_refs()
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ class Order(BaseModel):
status: Optional[str] = Field(alias="status", default=None)
complete: Optional[bool] = Field(alias="complete", default=None)


Order.update_forward_refs()
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ class Pet(BaseModel):
tags: Optional[List[Tag]] = Field(alias="tags", default=None)
status: Optional[str] = Field(alias="status", default=None)


Pet.update_forward_refs()
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ class Tag(BaseModel):
id: Optional[int] = Field(alias="id", default=None)
name: Optional[str] = Field(alias="name", default=None)


Tag.update_forward_refs()
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ class User(BaseModel):
phone: Optional[str] = Field(alias="phone", default=None)
user_status: Optional[int] = Field(alias="userStatus", default=None)


User.update_forward_refs()