Coder Social home page Coder Social logo

Comments (3)

vkuptcov avatar vkuptcov commented on June 15, 2024

@johanhenriksson did you find a workaround for this issue?
The only thing I've found it to define a kind of type alias to use in oneOf schema, e.g.

StringForDiscriminator:
  allOf:
    - $ref: '#/components/types/String'
    - type: object
      properties:
         kind:
           type: string

In that case we have 2 objects generated, one to be used in the case with discriminator, and another one without. But it looks a bit ugly, tbh.
Another solution is to just define a map and put data there

      additionalProperties:
        type: string

from ogen.

ernado avatar ernado commented on June 15, 2024

So having following schema

openapi: 3.0.3
info:
  title: Discriminator example
  description: Example
  version: 1.0.0
paths:
    /example:
      get:
        responses:
          200:
            description: OK
            content:
              "application/json":
                schema:
                    $ref: '#/components/schemas/Response'
components:
  schemas:
    Alpha:
      type: object
      required:
        - kind
        - variant
        - data
      properties:
        kind:
          type: string
        variant:
          type: string
        data:
          type: string
    Beta:
      type: object
      required:
        - kind
        - variant
        - value
      properties:
        kind:
          type: string
        variant:
          type: string
        value:
          type: string
    SumAuto:
      oneOf:
        - $ref: '#/components/schemas/Alpha'
        - $ref: '#/components/schemas/Beta'
    SumFirst:
      discriminator:
        propertyName: kind
        mapping:
          alpha: '#/components/schemas/Alpha'
          beta: '#/components/schemas/Beta'
      oneOf:
        - $ref: '#/components/schemas/Alpha'
        - $ref: '#/components/schemas/Beta'
    SumSecond:
      discriminator:
        propertyName: variant
        mapping:
          a: '#/components/schemas/Alpha'
          b: '#/components/schemas/Beta'
      oneOf:
        - $ref: '#/components/schemas/Alpha'
        - $ref: '#/components/schemas/Beta'
    Response:
      type: object
      required:
        - Auto
        - First
        - Second
        - Alpha
        - Beta
      properties:
        Auto:
          $ref: '#/components/schemas/SumAuto'
        First:
          $ref: '#/components/schemas/SumFirst'
        Second:
          $ref: '#/components/schemas/SumSecond'
        Alpha:
          $ref: '#/components/schemas/Alpha'
        Beta:
          $ref: '#/components/schemas/Beta'

Discriminator properties are removed from Alpha and Beta, but should not.

@tdakkota please take a look.

from ogen.

johanhenriksson avatar johanhenriksson commented on June 15, 2024

@vkuptcov I have not found a good workaround to this, no. We managed to do without it, but having the discriminator field on the generated types would make life easier.

from ogen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.