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

[BUG] [Dart] Problem with enum generation, when enum value is a symbol, like the character % #7867

Closed
5 tasks done
ka-zo opened this issue Nov 3, 2020 · 4 comments
Closed
5 tasks done

Comments

@ka-zo
Copy link

ka-zo commented Nov 3, 2020

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?
Description

There is a problem with the generated dart code for enums, as those enums in the openapi.json file, where the enum value is a special character, like %, will end up in the generated dart code with the name _.

openapi-generator version

4.3.1

Support file contents

The openapi.json file content is shown below. The part of the specification that results in the problematic dart code is components/schemas/PercentileUnit/enum.

{
    "openapi": "3.0.2",
    "info": {
        "title": "FastAPI",
        "version": "0.1.0"
    },
    "paths": {
        "/enum/": {
            "get": {
                "tags": [
                    "Administration"
                ],
                "summary": "Test Enum",
                "description": "Test get enum.",
                "operationId": "test_enum_enum__get",
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/PercentileValue"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "PercentileUnit": {
                "title": "PercentileUnit",
                "enum": [
                    "%"
                ],
                "type": "string",
                "description": "An enumeration."
            },
            "PercentileValue": {
                "title": "PercentileValue",
                "required": [
                    "unit",
                    "value"
                ],
                "type": "object",
                "properties": {
                    "unit": {
                        "$ref": "#/components/schemas/PercentileUnit"
                    },
                    "value": {
                        "title": "Value",
                        "maximum": 97,
                        "minimum": 3,
                        "type": "number",
                        "description": "Value of the percentile."
                    }
                }
            }
        }
    }
}

The flutterconfig-dart.json file is the following:

{
    "pubAuthor": "Agent Moo",
    "pubDescription": "Security level BBR (Burn Before Read)",
    "pubName": "PeekAMooOpenAPI",
    "browserClient": false,
    "useEnumExtension": true
}

The generated percentile_unit.dart model file that has an issue is the following:

part of PeekAMooOpenAPI.api;

class PercentileUnit {
  /// The underlying value of this enum member.
  final String value;

  const PercentileUnit._internal(this.value);

  /// An enumeration.
  static const PercentileUnit _ = const PercentileUnit._internal("%");

  static PercentileUnit fromJson(String value) {
    return new PercentileUnitTypeTransformer().decode(value);
  }
  
  static List<PercentileUnit> listFromJson(List<dynamic> json) {
    return json == null ? new List<PercentileUnit>() : json.map((value) => PercentileUnit.fromJson(value)).toList();
  }
}

class PercentileUnitTypeTransformer {

  dynamic encode(PercentileUnit data) {
    return data.value;
  }

  PercentileUnit decode(dynamic data) {
    switch (data) {
      case "%": return PercentileUnit._;
      default: throw('Unknown enum value to decode: $data');
    }
  }
}

The problem I have with the code above:

  /// An enumeration.
  static const PercentileUnit _ = const PercentileUnit._internal("%");

Dart code outside the generated dart package cannot reference enume value with PercentileUnit._. Somehow special characters, like % should be translated to a string, that can be used as valid parameter name in Dart, because currently the special characters are translated to an empty string. Most likely the problem is with the content of {{{name}}} in

static const {{classname}} {{{name}}} = const {{classname}}._internal({{{value}}});
and in
case {{{value}}}: return {{classname}}.{{{name}}};
.

Generation Details

The Dart code is generated on Windows with the following shell command.

java -jar openapi-generator-cli-4.3.1.jar generate -i .\openapi.json -g dart -o .\peekamoo-openapi -c flutterconfig-dart.json
Steps to reproduce

Execute shell command written above, using the support files included above, then check the generated dart code in .\peekamoo-openapi\lib\model\percentile_unit.dart.

Related issues/PRs

none

Suggest a fix

The content of {{{name}}} in

static const {{classname}} {{{name}}} = const {{classname}}._internal({{{value}}});
and in
case {{{value}}}: return {{classname}}.{{{name}}};
should not be simply _ when the enum value is a special character.

@auto-labeler
Copy link

auto-labeler bot commented Nov 3, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@kuhnroyal
Copy link
Contributor

This should now work on master.

@kuhnroyal
Copy link
Contributor

@wing328 I think this can be closed

@kuhnroyal
Copy link
Contributor

@ka-zo Can you try with 5.0.0?

@wing328 wing328 closed this as completed Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants