Skip to content

Omit readonly properties from PATCH requestContent #468

@rcbevans

Description

@rcbevans

If a schema is referenced as the requestContent to an API, the generated RequestBody should omit readonly properties.

Given a user:

export interface User {
  readonly id: number
  name: string
  readonly email: string
}

When this type is referenced to be the input to a PATCH request, the input argument should omit readonly properties.

It should be possible to do some Typescript magic to strip readonly properties, like mentioned in https://stackoverflow.com/a/49579497/2240706.

type IfEquals<X, Y, A=X, B=never> =
  (<T>() => T extends X ? 1 : 2) extends
  (<T>() => T extends Y ? 1 : 2) ? A : B;

type WritableKeys<T> = {
  [P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, P>
}[keyof T];

type ReadonlyKeys<T> = {
  [P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>
}[keyof T];

type OmitReadonly<T> = Pick<T, WritableKeys<T>>; 

It would then be a case of updating the generated code to declare the argument as OmitReadonly<ReferencedType>

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions