Skip to content

Polymorphism & Discriminator #1664

Closed

Description

@adrianiacobghiula

In the context of Polymorphism there are cases where the discriminator could have multiple values pointing to the same object

    CommunicationChannel:
      type: object
      oneOf:
      - $ref: '#/components/schemas/Phone'
      - $ref: '#/components/schemas/Email'
      - $ref: '#/components/schemas/PostAddress'
      discriminator:
        propertyName: type
        mapping:
          CALL: '#/components/schemas/Phone'
          SMS: '#/components/schemas/Phone'
          EMAIL: '#/components/schemas/Email'
          POST: '#/components/schemas/PostAddress'

as the generated code ignores one of the discriminators CALL or SMS
because the generated functions are called based on the object (FromPhone) and not on the discriminators

// FromPhone overwrites any union data inside the CommunicationChannel as the provided Phone
func (t *CommunicationChannel) FromPhone(v Phone) error {
	v.Type = "SMS"
	b, err := json.Marshal(v)
	t.union = b
	return err
}

The only work around I found is to create 2 new objects

    CommunicationChannel:
      type: object
      oneOf:
      - $ref: '#/components/schemas/CallPhone'
      - $ref: '#/components/schemas/SmsPhone'
      - $ref: '#/components/schemas/Email'
      - $ref: '#/components/schemas/PostAddress'
      discriminator:
        propertyName: type
        mapping:
          CALL: '#/components/schemas/CallPhone'
          SMS: '#/components/schemas/SmsPhone'
          EMAIL: '#/components/schemas/Email'
          POST: '#/components/schemas/PostAddress'
    CallPhone:
      allOf:
        - $ref: '#/components/schemas/Phone'
    SmsPhone:
      allOf:
        - $ref: '#/components/schemas/Phone'

Activity

  1. ishaish103 commented on Jul 4, 2024

    @ishaish103

    im having the exact same issue

  2. mgabeler-lee-6rs commented on Oct 2, 2024

    @mgabeler-lee-6rs
    Contributor

    Looks like a dupe of #1530

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions