Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for global enums #40

Closed
jkristia opened this issue Jul 15, 2021 · 1 comment
Closed

support for global enums #40

jkristia opened this issue Jul 15, 2021 · 1 comment
Assignees

Comments

@jkristia
Copy link
Contributor

jkristia commented Jul 15, 2021

It would be good to have support for enum defined at components/schemas level and used in data models by $ref

Global Enum model

Files.FileLocation:
 type: string
 enum:
   - deleted
   - location1
   - location2

Files.FileUploadReply:
 type: object
 properties:
   location:
     $ref: '#/components/schemas/Files.FileLocation'
   filename:
     type: string

Generated Python

class FilesFileLocation(OpenApiObject):
    DELETED = "deleted" # type: str
    LOCATION1 = "location1" # type: str
    LOCATION2 = "location2" # type: str
    
class FilesFileUploadReply(OpenApiObject):
    _TYPES = {
        "location": {
            "type": str,
            "enum": [
                "deleted"
                "location1",
                "location2"
            ],
        }
    }

    @property
    def location(self):
        # type: -> Union[Listeral["deleted"],Literal["location1"],Literal["location2"]]
        return self._get_property("location")

    @location.setter
    def location(self, value):
        # type: (Union[Listeral["deleted"],Literal["location1"],Literal["location2"]])
        self._set_property("location", value)

Sample usage

upload_reply = FilesFileUploadReply()
upload_reply.location = FilesFileLocation.LOCATION1
assert upload_reply.location == FilesFileLocation.LOCATION1
@ajbalogh
Copy link
Contributor

This is already supported. OpenAPIArt uses x-include to specifically inline code as opposed to $ref which expects global objects.

Declare the global enum as follows:

Files.FileLocation:
 type: string
 enum:
   - deleted
   - location1
   - location2

Inline the enum where needed in the model using the x-include extension.

Object1:
  type: object
  properties:
    global_enum_1:
      x-include:
      - '#/components/schemas/Files.FileLocation'
   
Object2:
  type: object
  properties:
    global_enum_2:
      x-include:
      - '#/components/schemas/Files.FileLocation'

SDK code will be generated on a per attribute basis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants