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

Schema allOf generation does not follow Swagger spec #1570

Open
MibbitTom opened this issue Aug 10, 2024 · 4 comments
Open

Schema allOf generation does not follow Swagger spec #1570

MibbitTom opened this issue Aug 10, 2024 · 4 comments
Labels
openapi_31 OpenAPI 3.1 related issue

Comments

@MibbitTom
Copy link

What are the steps to reproduce this issue?

I want to use Orval to generate an API that uses inheritance. But, my subclass' fields are being generated as optional even though I have marked them as required.

  1. Use Orval to generate models using the following Yaml:
openapi: 3.0.1
info:
  contact:
    email: xxx
    name: xxx
    url: xxx
  description: xxx
  title: xxx
  version: '0.1'
servers:
  - url: http://localhost:8080
    description: Generated server url
paths:
  /api/notifications/mine:
    get:
      operationId: getMyNotifications
      responses:
        '200':
          content:
            '*/*':
              schema:
                type: array
                items:
                  oneOf:
                    - $ref: '#/components/schemas/NewTicketNotification'
          description: OK
      summary: Get my notifications
      tags:
        - Notifications
components:
  schemas:
    NewTicketNotification:
      required:
        - ctime
        - id
        - ticketId
        - type
        - userId
      type: object
      allOf:
        - $ref: '#/components/schemas/Notification'
        - type: object
          properties:
            ticketId:
              type: integer
              format: int32
            userId:
              type: integer
              format: int32
    Notification:
      required:
        - ctime
        - id
        - type
      type: object
      properties:
        ctime:
          type: string
          format: date-time
        id:
          type: integer
          format: int32
        type:
          type: string
      discriminator:
        propertyName: type

What happens?

Orval generates the following:

export interface Notification {
  ctime: string;
  id: number;
  type: string;
}

export type NewTicketNotificationAllOf = {
  ticketId?: number;
  userId?: number;
};

export type NewTicketNotification = Notification & NewTicketNotificationAllOf;

What were you expecting to happen?

The fields in the subclass NewTicketNotificationAllOf should be required. E.g.

export type NewTicketNotificationAllOf = {
  ticketId: number;
  userId: number;
};

They actually are generated as required if I manually add required in the properties of the allOf. E.g.

NewTicketNotification:
      required:
        - ctime
        - id
        - ticketId
        - type
        - userId
      type: object
      allOf:
        - $ref: '#/components/schemas/Notification'
        - type: object
          required:
          - ticketId
          - userId
          properties:
            ticketId:
              type: integer
              format: int32
            userId:
              type: integer
              format: int32

But I shouldn't have to do that! NewTicketNotification already has ticketId declared as required. This should supersede the non-required property included in the allOf.

Any other comments?

Swagger's editor and codegen tool interpret the YAML correctly so I think I have interpreted the spec correctly! (See pictures)

I presume the parser needs a change to use the oneOf/allOf/anyOf key to make properties required (if needed) or leave them alone if they are already required.

image
image

What versions are you using?

Latest code built from Github.

System:
OS: Linux 6.5 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
CPU: (12) x64 AMD Ryzen 5 3600X 6-Core Processor
Memory: 2.69 GB / 15.55 GB
Container: Yes
Shell: 5.1.16 - /bin/bash

@melloware
Copy link
Collaborator

I think this is similar to #861

@melloware melloware added the openapi_31 OpenAPI 3.1 related issue label Aug 10, 2024
@MibbitTom
Copy link
Author

MibbitTom commented Aug 10, 2024

Sweet! Yes, that looks to be referring to the same issue.

Do you know if the maintainers are looking into that still? Pull request looks pretty dead...

(In case anyone else has the same issue, I'm just using input.override.transformer to manually remove the allOf properties for now.)

@melloware
Copy link
Collaborator

That PR is way out of date. Probably need a new Pr if you are interested .

@Samuel-Morgan-Tyghe
Copy link

Ditto on this as-well^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openapi_31 OpenAPI 3.1 related issue
Projects
None yet
Development

No branches or pull requests

3 participants