Skip to content

Bug: toJson() in Union type calls non-existent methods with 'Impl' suffix #411

@huxuezhi

Description

@huxuezhi

Steps to reproduce

Bug Report

Description:
When using json_serializer: freezed and generating Union types, the toJson() method calls methods with incorrect names.

Configuration:

  • json_serializer: freezed
  • use_freezed3: true

Generated Code:

Map<String, Object?> toJson() => switch (this) {
  UnionVariant1() => _$$UnionVariant1ImplToJson(this),  // Method doesn't exist
  UnionVariant2() => _$$UnionVariant2ImplToJson(this),  // Method doesn't exist
};


### Expected results

Expected Behavior: Should call _$UnionVariant1ToJson and _$UnionVariant2ToJson (without 'Impl' suffix) or use pattern matching to call the variant's toJson() method directly.

### Actual results

Actual Method Names in .g.dart:
Map<String, dynamic> _$UnionVariant1ToJson(UnionVariant1 instance)
Map<String, dynamic> _$UnionVariant2ToJson(UnionVariant2 instance)


### Your OpenApi snippet

`"/iam/sign-in": {
  "post": {
      "operationId": "iam.auth.signIn",
      "tags": [
          "IAM"
      ],
      "requestBody": {
          "required": true,
          "content": {
              "application/json": {
                  "schema": {
                      "anyOf": [
                          {
                              "type": "object",
                              "properties": {
                                  "type": {
                                      "const": "email"
                                  },
                                  "email": {
                                      "type": "string",
                                      "format": "email"
                                  },
                                  "password": {
                                      "type": "string"
                                  },
                                  "rememberMe": {
                                      "type": "boolean",
                                      "default": false
                                  }
                              },
                              "required": [
                                  "type",
                                  "email",
                                  "password"
                              ]
                          },
                          {
                              "type": "object",
                              "properties": {
                                  "type": {
                                      "const": "sms"
                                  },
                                  "phone": {
                                      "type": "string"
                                  },
                                  "smsCode": {
                                      "type": "integer",
                                      "minimum": -9007199254740991,
                                      "maximum": 9007199254740991
                                  },
                                  "rememberMe": {
                                      "type": "boolean",
                                      "default": false
                                  }
                              },
                              "required": [
                                  "type",
                                  "phone",
                                  "smsCode"
                              ]
                          }
                      ]
                  }
              }
          }
      },
      "responses": {
          "200": {
              "description": "OK",
              "content": {
                  "application/json": {
                      "schema": {
                          "type": "object",
                          "properties": {
                              "redirect": {
                                  "type": "boolean"
                              },
                              "token": {
                                  "type": "string"
                              },
                              "url": {
                                  "anyOf": [
                                      {
                                          "type": "string"
                                      },
                                      {
                                          "type": "null"
                                      }
                                  ]
                              },
                              "user": {
                                  "type": "object",
                                  "properties": {
                                      "id": {
                                          "type": "string"
                                      },
                                      "name": {
                                          "type": "string"
                                      },
                                      "email": {
                                          "type": "string"
                                      },
                                      "emailVerified": {
                                          "type": "boolean"
                                      },
                                      "image": {
                                          "anyOf": [
                                              {
                                                  "type": "string"
                                              },
                                              {
                                                  "type": "null"
                                              }
                                          ]
                                      },
                                      "role": {
                                          "anyOf": [
                                              {
                                                  "type": "string"
                                              },
                                              {
                                                  "type": "null"
                                              }
                                          ]
                                      },
                                      "banned": {
                                          "anyOf": [
                                              {
                                                  "type": "boolean"
                                              },
                                              {
                                                  "type": "null"
                                              }
                                          ]
                                      },
                                      "banReason": {
                                          "anyOf": [
                                              {
                                                  "type": "string"
                                              },
                                              {
                                                  "type": "null"
                                              }
                                          ]
                                      },
                                      "banExpires": {
                                          "anyOf": [
                                              {
                                                  "type": "string",
                                                  "format": "date-time",
                                                  "x-native-type": "date"
                                              },
                                              {
                                                  "type": "null"
                                              }
                                          ]
                                      },
                                      "createdAt": {
                                          "type": "string",
                                          "format": "date-time",
                                          "x-native-type": "date"
                                      },
                                      "updatedAt": {
                                          "type": "string",
                                          "format": "date-time",
                                          "x-native-type": "date"
                                      }
                                  },
                                  "required": [
                                      "id",
                                      "name",
                                      "email",
                                      "emailVerified",
                                      "image",
                                      "role",
                                      "banned",
                                      "banReason",
                                      "banExpires",
                                      "createdAt",
                                      "updatedAt"
                                  ],
                                  "additionalProperties": false
                              }
                          },
                          "required": [
                              "redirect",
                              "token",
                              "user"
                          ]
                      }
                  }
              }
          },
      }
  }
}`

### Code sample

<details><summary>Code sample</summary>

```dart
[Paste your code here]

Logs

Logs
[Paste your logs here]

Dart version and used packages versions

Dart version
[Paste your output here]
Packages version
[Paste your output here]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions