Skip to content

[BUG][PYTHON][PLUSS OTHERS] Enum variable names not update in all python converters #16560

Closed
@mwilby

Description

@mwilby

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)

There is no need for a detailed report. This is a simple problem with a simple fix and it applies to all versions.

Description

There is a problem with the Python family of converters when processing enums.
Basically none of them substitutes the enum var extensions.

So using a simple scheme definition

maitred_format:
   type: integer
   title: Message Format
   enum:
     - 1
     - -1
     - 0
   x-enum-varnames:
     - JSON
     - Unknown
     - SIBEC
   x-enum-descriptions:
     - JSON Descriptions
     - Unknown stuff
     - SIBEC Description

The python converter creates this:

class MaitredFormat(int, Enum):
    """
    Encoding format of Payload.
    """

    """
    allowed enum values
    """
    NUMBER_1 = 1
    NUMBER_MINUS_1 = -1
    NUMBER_0 = 0

    @classmethod
    def from_json(cls, json_str: str) -> MaitredFormat:
        """Create an instance of MaitredFormat from a JSON string"""
        return MaitredFormat(json.loads(json_str))

If you look at the generated model info, the relevant part is found to be.

{
  ...,
  "model" : {
    ...,
    "classname" : "MaitredFormat",
    ...,
    "allowableValues" : {
      "values" : [ 1, -1, 0 ],
      "enumVars" : [ {
        "name" : "NUMBER_1",
        "isString" : false,
        "enumDescription" : "JSON Descriptions",
        "value" : "1"
      }, {
        "name" : "NUMBER_MINUS_1",
        "isString" : false,
        "enumDescription" : "Unknown stuff",
        "value" : "-1"
      }, {
        "name" : "NUMBER_0",
        "isString" : false,
        "enumDescription" : "SIBEC Descriptions.",
        "value" : "0"
      } ]
    },
    ...
  }
}

This is because updateEnumVarsWithExtensions() is never called and to fix it, its a simple matter of adding

    @Override
    public ModelsMap postProcessModels(ModelsMap objs) {
        // process enum in models
        return postProcessModelsEnum(objs);
    }

to the relevant Python????Codegen.java module.

There are a fair number of other language models that don't include a postProcessModels override, not sure if it matters, or not, but it probably does.

Taking a language that does include the call, in this case, csharp, you generate the correct form of enum, i.e.

namespace Org.OpenAPITools.Model
{
    public enum MaitredFormat
    {
        JSON = 1,
        Unknown = -1,
        SIBEC = 0,
    }
}

This is because the model has been processed and looks like this.

{
  ...,
  "model" : {
    ...,
    "classname" : "MaitredFormat",
    ...,
    "allowableValues" : {
      "values" : [ 1, -1, 0 ],
      "enumVars" : [ {
        "name" : "JSON",
        "isString" : false,
        "enumDescription" : "JSON Descriptions",
        "value" : "1"
      }, {
        "name" : "Unknown",
        "isString" : false,
        "enumDescription" : "Unknown stuff",
        "value" : "-1"
      }, {
        "name" : "SIBEC",
        "isString" : false,
        "enumDescription" : "SIBEC Descriptions.",
        "value" : "0"
      } ]
    },
    ...
  }
}
Suggest a fix

Repeat adding

    @Override
    public ModelsMap postProcessModels(ModelsMap objs) {
        // process enum in models
        return postProcessModelsEnum(objs);
    }

to the relevant Python????Codegen.java module.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions