OpenAPITools / OpenAPITools/openapi-generator

[BUG] Dart-dio serialization with discriminator

Open
#12,749 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

So I'm trying to implement open api discriminator for serialization and deserialization but look like it doesn't work at all.

openapi-generator version

5.2.1

OpenAPI declaration file content
openapi: 3.0.3
info:
  title: KiWi API V1 Refined
  description: This is the refined version of Kiwi API
  contact:
    email: dev@kiwi-bop.com
  license:
    name: BSD License
  version: v1
paths:
  /api/v1/vehicules:
    post:
      requestBody:
        content:
          application/json:
            schema:
              discriminator:
                propertyName: powerSource
                mapping:
                  electricity: '#/components/schemas/ElectricVehicle'
                  gasoline: '#/components/schemas/FueledVehicle'
                  human-energy: '#/components/schemas/PedaledVehicle'
              anyOf:
                - $ref: '#/components/schemas/ElectricVehicle'
                - $ref: '#/components/schemas/FueledVehicle'
                - $ref: '#/components/schemas/PedaledVehicle'
      responses:
        '200':
          description: all good
          content:
            application/json:
              schema:
                type: array
                items:
                  discriminator:
                    propertyName: powerSource
                    mapping:
                      electricity: '#/components/schemas/ElectricVehicle'
                      gasoline: '#/components/schemas/FueledVehicle'
                      human-energy: '#/components/schemas/PedaledVehicle'
                  anyOf:
                    - $ref: '#/components/schemas/ElectricVehicle'
                    - $ref: '#/components/schemas/FueledVehicle'
                    - $ref: '#/components/schemas/PedaledVehicle'
components:
  schemas:
    Vehicle:
      type: object
      description: Vehicle
      discriminator:
        propertyName: powerSource
        mapping:
          electricity: '#/components/schemas/ElectricVehicle'
          gasoline: '#/components/schemas/FueledVehicle'
          human-energy: '#/components/schemas/PedaledVehicle'
      properties:
        vehicleType:
          description: The type of vehicle.
          type: string
          example: bicycle
        idealTerrain:
          type: string
          example: roads
        topSpeed:
          description: The top speed in kilometers per hour rounded to the nearest integer.
          type: integer
          example: 83
        powerSource:
          description: How is the vehicle powered.
          type: string
          example: pedaling
    FueledVehicle:
      allOf:
        - $ref: '#/components/schemas/Vehicle'
        - type: object
          title: Gas-powered Vehicle
          properties:
            range:
              description: The 95th percentile range of a trip in kilometers.
              type: integer
              example: 100
            tankCapacity:
              type: number
              format: double
              description: Capacity of the fuel tank in gallons.
            milesPerGallon:
              type: number
              format: double
              description: Miles per gallon on the highway.
    PedaledVehicle:
      allOf:
        - $ref: '#/components/schemas/Vehicle'
        - type: object
          description: Pedaled Vehicle
          properties:
            range:
              description: The 95th percentile range of a trip in kilometers.
              type: integer
              example: 100
    ElectricVehicle:
      allOf:
        - $ref: '#/components/schemas/Vehicle'
        - type: object
          description: Electric Vehicle
          properties:
            range:
              description: The 95th percentile range of a trip in kilometers.
              type: integer
              example: 100
            chargeSpeed:
              description: In range kilometers per hour.
              type: integer
            chargeAmps:
              description: Amps recommended for charging.
              type: integer
            chargeVoltage:
              description: Voltage recommended for charging.
              type: integer
Generation Details

It generates all kind of all_of weird models.
Capture d’écran 2022-06-30 à 17 02 40

But also it doesn't compile at all in the end cause it tries to use non existing models:

lib/src/serializers.dart:36:45: Error: Undefined name 'AnyOfElectricVehicleFueledVehiclePedaledVehicle'.
        const FullType(BuiltList, [FullType(AnyOfElectricVehicleFueledVehiclePedaledVehicle)]),
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Is discriminator something supported by this generator? Not totally sure by the look of what is generated.

cc @josh-burton

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

Reproduce the Dart-dio generation from the supplied OpenAPI declaration and inspect the generated models plus lib/src/serializers.dart. Start with the serializer reference to AnyOfElectricVehicleFueledVehiclePedaledVehicle, then verify that generation produces existing model types and that the resulting client compiles with discriminator serialization and deserialization.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.