Skip to content

Discriminated subclasses fields doesn't have MappableField for correct key name. #436

Description

@danialb007

Steps to reproduce

  1. Use DartMappable -> json_serializer: dart_mappable
  2. Have some schema with oneOf, discriminator setup, but make sure fields have snake case like this -> is_active
  3. Start the generation and use the classes. you will have an error like this ->
    [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: MapperException: Failed to decode (BaseType).encounters(List<SubClassTypeSealed>).item(SubClassTypeSealed).isActive: Parameter isActive is missing.

Fix Suggestion

Normal classes and types (Not discriminated) are fine and don't have this problem.
Adding this @MappableField(key: 'is_active') above the fields in the mappable classes (annotated with @MappableClass()) before code generation would fix it.

I think this code in the file lib/src/generator/templates/dart_dart_mappable_dto_template.dart:200 is related.

/// if jsonKey is different from the name
String _jsonKey(UniversalType t) {
  if (t.jsonKey == null || t.name == t.jsonKey) {
    return '';
  }
  return "${indentation(2)}@MappableField(key: '${protectJsonKey(t.jsonKey)}')\n";
}

Also changing line lib/src/generator/templates/dart_dart_mappable_dto_template.dart:691 and adding '_jsonKey' fixes this, but not sure if this is the best fix.

'${_jsonKey(prop)}${indentation(2)}@override\n${indentation(2)}final ${_renameUnionTypes(prop.toSuitableType(ProgrammingLanguage.dart, useMultipartFile: useMultipartFile))} ${prop.name};',

Expected results

No errors occurring.

Actual results

Mentioned in the steps to reproduce section.

Your OpenApi snippet

Here is an array that has different types inside.

"different_types_array": {
    "title": "DifferentTypesArray",
    "type": "array"
    "items": {
        "discriminator": {
            "mapping": {
                "a": "#/components/schemas/SubSchemaA",
                "b": "#/components/schemas/SubSchemaB",
                "c": "#/components/schemas/SubSchemaC"
            },
            "propertyName": "resource_type"
        },
        "oneOf": [
            {
                "$ref": "#/components/schemas/SubSchemaA"
            },
            {
                "$ref": "#/components/schemas/SubSchemaB"
            },
            {
                "$ref": "#/components/schemas/SubSchemaC"
            }
        ]
    },
},

Code sample

Code sample
switch (element) {
  SubClassA a => a,
  SubClassB b => b,
  SubClassC c => c,
},

Logs

Logs
[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: MapperException: Failed to decode (BaseType).encounters(List<SubClassTypeSealed>).item(SubClassTypeSealed).isActive: Parameter isActive is missing.

Dart version and used packages versions

Dart version
Dart SDK version: 3.10.4 (stable) (Tue Dec 9 00:01:55 2025 -0800) on "macos_arm64"
Packages version
swagger_parser: ^1.42.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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