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

Required properties across allOfs don't work #1670

Open
Edward-Upton opened this issue Oct 24, 2024 · 1 comment
Open

Required properties across allOfs don't work #1670

Edward-Upton opened this issue Oct 24, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Edward-Upton
Copy link

Edward-Upton commented Oct 24, 2024

What are the steps to reproduce this issue?

  1. Generate an OpenAPI schema that has a component with optional properties, then try and set those properties as required in other item in an allOf, e.g:
components:
  schemas:
    ContractPartial:
      type: object
      properties:
        uuid:
          type: string
        description:
          type: string
        start_date:
          type: string
          format: date
        end_date:
          type: string
          format: date
    Contract:
      type: object
      allOf:
        - $ref: '#/components/schemas/ContractPartial'
        - type: object
          properties:
            active:
              type: boolean
              description: If this contract is current active.
          required:
            - description
            - end_date
            - start_date
            - uuid
  1. Generate the Orval code, and these types will be like the following:
export type ContractPartial = {
  uuid?: string;
  description?: string;
  start_date?: string;
  end_date?: string;
}

export type ContractAllOf = {
  active: boolean;
};

export type Contract = ContractPartial & ContractAllOf;

What happens?

Notice that the outputted generated types do not specify that uuid, description, start_date and end_date should be required.

What were you expecting to happen?

Like other OpenAPI generators (e.g. oapi-codegen generate these types as required). In this case, it potentially would look something like:

type RequiredKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;

export type ContractPartial = {
  uuid?: string;
  description?: string;
  start_date?: string;
  end_date?: string;
}

export type ContractAllOf = {
  active: boolean;
};

export type Contract = RequiredKeys<ContractPartial & ContractAllOf, "uuid" | "description" | "start_date" | "end_date" | "area">;

## What versions are you using?

System:
OS: Linux 4.18 Rocky Linux 8.8 (Green Obsidian)
CPU: (4) x64 Intel(R) Xeon(R) Gold 6132 CPU @ 2.60GHz
Memory: 2.16 GB / 11.45 GB
Container: Yes
Shell: 3.6.1 - /usr/bin/fish
npmPackages:
axios: ^1.3.6 => 1.3.6
msw: ^2.2.0 => 2.2.0
orval: ^7.2.0 => 7.2.0

@melloware melloware added the enhancement New feature or request label Oct 24, 2024
@jorge-ui
Copy link

Yes, I'm also having this issue.
Using allOfs to define a type in a OAS spec file produces a union type when generating TypeScript:

type MyUnionType = SomeType & SomeOtherType;

This is great, but the issue is that the generated types are not completely inheriting the properties marked as required despite being defined in the specs, and are instead being generated as optional, this means extra care from the dev to verify the type's usage and handle them to satisfy the ts compiler, minor inconvenience and somewhat annoying, though not a dealbreaker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants