Skip to content

Union intermediate types don't include the variant name #1038

@MJDSys

Description

@MJDSys

Expected Behavior

I'm trying to generate a Union enumeration where some of the variants use the same type for their contents. In this case, the enumeration represents a state machine where some states share the same contents. I want the states to be separate though as they represent different points in the machine. A minimal example looks like:

#[derive(Object)]
struct Details {
    pub field: i32,
}

#[derive(Union)]
#[oai(discriminator_name = "state")]
#[oai(one_of)]
enum State {
    Variant1(Details),
    Variant2(Details),
}

Ideally the generated OpenAPI definition would still fully distinguish between these states.

Actual Behavior

The actual generated schema generates one type for both variants, which makes them indistinguishable and not a valid oneOf OpenAPI type. The generated schema looks like:

components:
  schemas:
    Details:
      type: object
      title: Details
      required:
      - field
      properties:
        field:
          type: integer
          format: int32
    State:
      type: object
      oneOf:
      - $ref: '#/components/schemas/State_Details'
      - $ref: '#/components/schemas/State_Details'
      discriminator:
        propertyName: state
        mapping:
          Variant1: '#/components/schemas/State_Details'
          Variant2: '#/components/schemas/State_Details'
    State_Details:
      allOf:
      - type: object
        required:
        - state
        properties:
          state:
            type: string
            enum:
            - Variant2
            example: Variant2
      - $ref: '#/components/schemas/Details'

Steps to Reproduce the Problem

I've created a full sample here: https://github.com/MJDSys/poem-repo

The issue seems to be here:

::std::format!("{}_{}", <Self as #crate_name::types::Type>::name(), <#object_ty as #crate_name::types::Type>::name())

Where the schema name is generated based on the type of the interior struct. If it is an acceptable change, I could submit a PR changing this to be based on the variant's name in the enumeration which would be unique.

Specifications

  • Version: 5.1.14
  • Platform: Linux
  • Subsystem: poem-openapi

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions