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

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

Open
ahmednfwela opened this issue May 16, 2023 · 0 comments

Comments

@ahmednfwela
Copy link
Contributor

ahmednfwela commented May 16, 2023

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)

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

No branches or pull requests

1 participant