OpenAPITools / OpenAPITools/openapi-generator

[BUG] [dart-dio] Fix behavior for schemas with self referencing discriminator

Open
#15,517 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description
"Person": {
  "type": "object",
  "properties": {
    "Id": {
      "type": "string",
      "format": "uuid"
    },         
    "type": {
      "title": "Discriminator Type",
      "type": "string"
    }
  },
  "additionalProperties": false,
  "discriminator": {
    "propertyName": "type",
    "mapping": {
      //Self reference
      "person": "#/components/schemas/Person",
      "author": "#/components/schemas/Author"
    }
  }
},

leads to this code:

@override
Object serialize(
  Serializers serializers,
  Person object, {
  FullType specifiedType = FullType.unspecified,
}) {
  if (object is Author) {
    return serializers.serialize(object,
        specifiedType: FullType(Author))!;
  }
  // this block should be removed entirely, leads to recursion
  if (object is Person) {
    return serializers.serialize(object,
        specifiedType: FullType(Person))!;
  }
  //====================================
  return _serializeProperties(serializers, object,
          specifiedType: specifiedType)
      .toList();
}

and

switch (discValue) {
  case r'author':
    return serializers.deserialize(serialized,
            specifiedType: FullType(Author))
        as Author;
  // this block should be removed entirely, leads to recursion
  case r'person':
    return serializers.deserialize(serialized,
            specifiedType: FullType(Person))
        as Person;
  //====================================
  default:
    return serializers.deserialize(serialized,
            specifiedType: FullType($Person))
        as $Person;
}

and

extension PersonDiscriminatorExt
    on Person {
  String? get discriminatorValue {
    if (this is Author) {
      return r'author';
    }
/// This check should be removed, and instead of returning null at the end, should return r'person'
    if (this is Person) {
      return r'person';
    }
    return null;
  }
}

extension PersonBuilderDiscriminatorExt
    on PersonBuilder {
  String? get discriminatorValue {
    if (this is AuthorBuilder) {
      return r'author';
    }
/// This check should be removed, and instead of returning null at the end, should return r'person'
    if (this is PersonBuilder) {
      return r'person';
    }
    return null;
  }
}
openapi-generator version

6.6.0

I plan on fixing this in an upcoming PR, will introduce a new vendor extension called fallback case, which will be the concrete class in this case ($Person) and remove self references from mappings in java

@jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the supplied self-referencing Person schema with openapi-generator 6.6.0 and inspect the generated dart-dio serializer, deserializer, and discriminator extensions shown in the issue. Confirm that the self-reference does not recurse and that the concrete fallback class handles the discriminator value without duplicate self-reference checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.