Forum Discussion

zheenbek's avatar
zheenbek
New Member
2 years ago

Reusable enums in python codegen

Hi,

I have the following issue when generating a python code out of swagger. When I declare an enum in place i.e:

paths:
  /items:
    get:
      parameters:
        - in: query
          name: sort
          description: Sort order
          schema:
            $ref: '#/definitions/RequestBody'
definitions:
  RequestBody:
      enumField:
        type: string
        enum:
          - ENUM_FIRST
          - ENUM_SECOND
          - ENUM_THIRD

It generates the following python RequestBody model:

class RequestBody(object):
    """
    NOTE: This class is auto generated by the swagger code generator program.
    Do not edit the class manually.
    """
    def __init__(self, enum_field=None):
        """
        RequestBody - a model defined in Swagger

        :param dict swaggerTypes: The key is attribute name
                                  and the value is attribute type.
        :param dict attributeMap: The key is attribute name
                                  and the value is json key in definition.
        """
        self.swagger_types = {
            'enum_field': 'str'
        }

        self.attribute_map = {
            'enum_field': 'enumField'
        }

        self._enum_field = enum_field

    @property
    def enum_field(self):
        """
        :return: The enum_field of this RequestBody.
        :rtype: str
        """
        return self.enum_field

    @enum_field.setter
    def enum_field(self, enum_field):
        """
        :param enum_field: The enum_field of this RequestBody.
        :type: str
        """
        allowed_values = ["ENUM_FIRST", "ENUM_SECOND", "ENUM_THIRD"]
        if enum_field not in allowed_values:
            raise ValueError(
                "Invalid value for `enum_field` ({0}), must be one of {1}"
                .format(enum_field, allowed_values)
            )

        self._enum_field = enum_field

 This all looks good. But when I try the same thing with reusable enums it generates an empty object with no option to initialise with a valid value i.e.:
 

paths:
  /items:
    get:
      parameters:
        - in: query
          name: sort
          description: Sort order
          schema:
            $ref: '#/definitions/RequestBody'
definitions:
  RequestBody:
    type: object
    properties:
      enumField:
        $ref: '#/definitions/ReusableEnum'
  ReusableEnum:
    type: string
    enum:
      - ENUM_FIRST
      - ENUM_SECOND
      - ENUM_THIRD

The above definitions generates these models:

class RequestBody(object):
    """
    NOTE: This class is auto generated by the swagger code generator program.
    Do not edit the class manually.
    """
    def __init__(self, enum_field=None):
        """
        RequestBody - a model defined in Swagger

        :param dict swaggerTypes: The key is attribute name
                                  and the value is attribute type.
        :param dict attributeMap: The key is attribute name
                                  and the value is json key in definition.
        """
        self.swagger_types = {
            'enum_field': 'ReusableEnum'
        }

        self.attribute_map = {
            'enum_field': 'ReusableEnum'
        }

        self._enum_field = enum_field

    @property
    def enum_field(self):
        """
        :return: The enum_field of this RequestBody.
        :rtype: ReusableEnum
        """
        return self.enum_field

    @enum_field.setter
    def enum_field(self, enum_field):
        """
        :param enum_field: The enum_field of this RequestBody.
        :type: ReusableEnum
        """
        self._enum_field = enum_field

And it generates the following model for the ReusableEnum :

class ReusableEnum(object):
    """
    NOTE: This class is auto generated by the swagger code generator program.
    Do not edit the class manually.
    """
    def __init__(self):
        """
        ReusableEnum - a model defined in Swagger

        :param dict swaggerTypes: The key is attribute name
                                  and the value is attribute type.
        :param dict attributeMap: The key is attribute name
                                  and the value is json key in definition.
        """
        self.swagger_types = {
            
        }

        self.attribute_map = {
            
        }

As you can see the generated ReusableEnum does not have any enum fields nor allowed_values things. How would I even initialise such an enum object with no values being passed on a constructor?

For now the only workaround is not to use reusable enums and go with the first approach, but sometimes I want to reuse the same enum model, rather than creating different models for same enums.

Is this is a bug or do I not understand something? S.O.S.

No RepliesBe the first to reply