swagger_parser icon indicating copy to clipboard operation
swagger_parser copied to clipboard

Wrong JsonKey generated

Open winren9 opened this issue 9 months ago • 2 comments

Steps to reproduce

generate Swagger with the following config:

swagger_parser:
  enums_to_json: true
  schema_path: api.json 
  output_directory: lib/api 
  language: dart Default: dart
  json_serializer: freezed

global_options:
 freezed:
   runs_before:
     - json_serializable
 json_serializable:
   runs_before:
     - retrofit_generator

The JsonKey is wrong. It should start with a lowercase letter or even be omitted. I checked the previous versions and it seems to be related to the 1.18.0 version

Expected results

@Freezed()
class MenuItem with _$MenuItem {
  const factory MenuItem({
    required String title,
    required LinkOptions linkOptions,
  }) = _MenuItem;
  
  factory MenuItem.fromJson(Map<String, Object?> json) => _$MenuItemFromJson(json);
}

Actual results

@Freezed()
class MenuItem with _$MenuItem {
  const factory MenuItem({
    required String title,
    @JsonKey(name: 'LinkOptions')
    required LinkOptions linkOptions,
  }) = _MenuItem;
  
  factory MenuItem.fromJson(Map<String, Object?> json) => _$MenuItemFromJson(json);
}

Your OpenApi snippet

  "components": {
    "schemas": {
      "MenuItem": {
        "type": "object",
        "required": ["title"],
        "properties": {
          "title": { "type": "string", "example": "Test 123" },
          "linkOptions": { "$ref": "#/components/schemas/linkOptions" }
        }
      },
      "linkOptions": {
        "type": "object",
        "required": ["type", "title"],
        "properties": {
          "type": {
            "enum": ["internal", "external"],
            "type": "string",
            "example": "internal"
          }
        }
      }
    }
  },

Code sample

No response

Logs

No response

Dart version and used packages versions

Dart version
Dart 3.7.2
Packages version
  swagger_parser: 1.22.0

winren9 avatar Mar 24 '25 07:03 winren9

Hey @winren9

The issue comes from here

Image

It renames types and prop by accident

Image

Probably the best way is not to alter file contents but handle proper class naming while creating UniversalType

May the contributor force be with you I had a time to only investigate

vasilich6107 avatar Jul 07 '25 12:07 vasilich6107

I also encountered the same problem. Is this a bug?

Du1129 avatar Sep 10 '25 06:09 Du1129