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

Enum maps in swagger.g.dart are missing 'enums.' prefix to correctly point at imported enums #188

Closed
point-source opened this issue Jul 19, 2021 · 11 comments · Fixed by #199

Comments

@point-source
Copy link

point-source commented Jul 19, 2021

api_name.swagger.dart file contains these lines:

import 'lasso.enums.swagger.dart' as enums;
export 'lasso.enums.swagger.dart';

and when referencing an enum in this same file (notice the preceding enums.):

@JsonKey(name: 'my_enum')
final enums.MyEnum? myEnum;

but the part file api_name.swagger.g.dart attempts to access them this way:

const _$MyEnum = {
  MyEnum.swaggerGeneratedUnknown: 'swaggerGeneratedUnknown',
  MyEnum.hourly: 'hourly',
  MyEnum.daily: 'daily',
  MyEnum.weekly: 'weekly',
};

This gives the following errors:

Const variables must be initialized with a constant value.
Try changing the initializer to be a constant expression.dartconst_initialized_with_non_constant_value

Undefined name 'MyEnum'.
Try correcting the name to one that is defined, or defining the name.

By manually adding enums. preceding each MyEnum reference, the errors are corrected. Am I missing a build option / flag or is this incorrectly generated the swagger.g.dart enum map code?

My working code looks like this:

const _$MyEnum = {
  enums.MyEnum.swaggerGeneratedUnknown: 'swaggerGeneratedUnknown',
  enums.MyEnum.hourly: 'hourly',
  enums.MyEnum.daily: 'daily',
  enums.MyEnum.weekly: 'weekly',
};

Unfortunately, there are far too many references for me to do this by hand. And besides, if this is a real bug, we should fix it at the source. Thanks in advance!

point-source added a commit to point-source/dart_lasso that referenced this issue Jul 19, 2021
@Vovanella95
Copy link
Collaborator

Hi @point-source ,

json_serializable file with .g postfix should not know about enums.

We add @jsonkey(fromJson: customMethod) for enum properties. If it not happend - please share swagger file with error and I will fix generator.

@point-source
Copy link
Author

@miguelcmedeiros
Copy link

I'm having a similar issue where enums defined in the swagger file are mentioned the *.swagger.dart file, but the correspondent enum is not generated in *.enums.swagger.dart file.

In this example, I have a schema model with a property icon, which has a string property called name and an enum property called kind:

{
  "openapi": "3.0.1",
  "info": {
    "title": "API V1",
    "version": "v1"
  },
  "components": {
    "schemas": {
      "model": {
        "type": "object",
        "properties": {
          "title": {
            "type": "string",
            "nullable": true
          },
          "icon": {
            "type": "object",
            "properties": {
              "kind": {
                "type": "string",
                "enum": [
                  "icon",
                  "image"
                ]
              },
              "name": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}

The *.swagger.dart will mention enums.Model$IconKind but the *.enums.swagger.dart file is not generated.

When a new schema is created for icon and referenced as in model, then the enum is properly generated:

{
  "openapi": "3.0.1",
  "info": {
    "title": "API V1",
    "version": "v1"
  },
  "components": {
    "schemas": {
      "icon": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "string",
            "enum": [
              "icon",
              "image"
            ]
          },
          "name": {
            "type": "string"
          }
        }
      },
      "model": {
        "type": "object",
        "properties": {
          "title": {
            "type": "string",
            "nullable": true
          },
          "icon": {
            "$ref": "#/components/schemas/icon"
          }
        }
      }
    }
  }
}

This could be a workaround for some cases, but it would be great if it could handle both cases.

@point-source
Copy link
Author

This issue is affecting the Plaid spec as well: https://github.com/plaid/plaid-openapi/blob/master/2020-09-14.yml

Tested with the 2.0.9 branch.

@Vovanella95
Copy link
Collaborator

I will check it

@fryette
Copy link
Contributor

fryette commented Jul 27, 2021

@point-source could you please confirm that issue was reproduced with the latest 2.0.8 version?

@point-source
Copy link
Author

@fryette Unfortunately, when I try to build with 2.0.8 I encounter this error:

[SEVERE] json_serializable:json_serializable on lib/generated/plaid_service.swagger.dart:

Could not generate `fromJson` code for `ytdEarnings`.
To support the type `Null` you can:
* Use `JsonConverter`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
package:dart_plaid/generated/plaid_service.swagger.dart:17364:15
      ╷
17364 │   final Null? ytdEarnings;
      │               ^^^^^^^^^^^
      ╵

which I believe is because 2.0.8 (at least) does not handle this type:

"ytd_earnings": {
                        "allOf": [
                            {
                                "$ref": "#/components/schemas/PaystubYTDDetails"
                            }
                        ]
                    }

So I manually removed that property as a test and it did build though the original enum related issue remains the same. Hopefully that helps. Let me know if there's anything else I can do.

@Vovanella95
Copy link
Collaborator

Ok let me see. I've fixed next swagger file
https://github.com/plaid/plaid-openapi/blob/master/2020-09-14.yml

Anything else to fix in this issue?

@point-source @miguelcmedeiros

@point-source
Copy link
Author

Awesome! Thank you so much. I can test this in about an hour.

@Vovanella95
Copy link
Collaborator

@point-source test it from branch for now. Tomorrow I will release it to pub.dev. just need to test changes a bit

@point-source
Copy link
Author

Yep, this appears fixed in the 2.0.8_plus branch. Thanks!

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