orval icon indicating copy to clipboard operation
orval copied to clipboard

Inline properties in `AllOf` that is not a ref

Open StefanBRas opened this issue 1 year ago • 7 comments

What are the steps to reproduce this issue?

Given the spec

openapi: 3.0.3
info:
  title: test
  version: 0.1.0
paths: {}
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
    UserWithName:
      allOf:
        - $ref: '#/components/schemas/User'
        - type: object
          properties:
            name:
              type: string

I get

// user.ts
export interface User {
  id?: string;
}

// userWithNameAllOf.ts
export type UserWithNameAllOf = {
  name?: string;
};

// userWithName.ts
import type { User } from './user';
import type { UserWithNameAllOf } from './userWithNameAllOf';

export type UserWithName = User & UserWithNameAllOf;

Which is correctly, by the AllOf is kinda redundant.

What were you expecting to happen?

// user.ts
export interface User {
  id?: string;
}

export type UserWithNameAllOf = 

// userWithName.ts
import type { User } from './user';

export type UserWithName = User & {name?: string};

Which is equivalent, but nicer. …

Any logs, error output, etc?

Any other comments?

I can see that some people might want to have it as a separate type, but the names quickly become long and ugly.

Maybe it should be a configuration option?

What versions are you using?

  System:
    OS: macOS 14.6.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 69.92 MB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  npmPackages:
    @tanstack/react-query: ^4.22.0 => 4.36.1 
    axios: ^1.7.4 => 1.7.7 
    orval: 7.1.0 => 7.1.0 

StefanBRas avatar Sep 16 '24 11:09 StefanBRas

The current format has the advantage that you can import just one type at a time, like this:

import { UserWithNameAllOf } from '..user.ts'.

soartec-lab avatar Sep 16 '24 23:09 soartec-lab

@soartec-lab good point. I would lean towards "won't fix"

melloware avatar Sep 16 '24 23:09 melloware

You can say Omit<UserWithName, keyof User> to get that type instead, which I also think is nicer because it clearly shows that I want all fields in UserWithName that isn't in User whereas the name UserWithNameAllOf doesn't carry any information.

In our project, we have around 30 of these AllOf and all of them are a single discriminator field that gets its own AllOf type which is just noise here.

Wold you accept a PR with a configuration for it?

StefanBRas avatar Sep 17 '24 07:09 StefanBRas

Yes PR is welcome @StefanBRas !

melloware avatar Sep 17 '24 11:09 melloware

@StefanBRas have you started on this? Otherwise I'm interested in picking it up in the future

matthewaptaylor avatar Feb 26 '25 21:02 matthewaptaylor

@matthewaptaylor go ahead

melloware avatar Feb 26 '25 22:02 melloware

Hey! Ah sorry, I started but then got kinda stuck on the testing setup. I should probably have reported back. Feel free to pick it up

StefanBRas avatar Feb 27 '25 07:02 StefanBRas