hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.39k stars 107 forks source link

`type: json` generates `json` type in `types.gen.ts` #1166

Closed esfox closed 1 month ago

esfox commented 1 month ago

Description

If an object property in the spec is type: json, the generated type of that property in types.gen.ts is json, which is not a valid type in TypeScript, if I'm not mistaken. The linter suggests JSON, but I think the json type in the spec translates to a JSON string, and if so, the generated type of the field should be string.

Expected

export type SomeObject = {
  name: string;
  characteristics: string;
}

Actual

export type SomeObject = {
  name: string;
  characteristics: json;
}

Reproducible example or configuration

No response

OpenAPI specification (optional)

SomeObject:
  type: object
    properties:
      name:
        type: string
      characteristics:
        type: json

System information (optional)

No response

mrlubos commented 1 month ago

Hey @esfox, is that a valid type? Why not make it string to begin with?

esfox commented 1 month ago

Actually I think you're right. It doesn't say here and in the Swagger docs too that json is a valid type.

I tried changing the type in the spec to object and it did translate to a valid type ({ [key: string]: unknown; }).

Gonna close this issue. Thanks for the timely response!

mrlubos commented 1 month ago

No worries. Are you constructing the schema by hand? If so, why?

esfox commented 1 month ago

Actually no. It's just that in our backend, we actually use the Swagger module of NestJS and we defined a property of a class as @ApiProperty({ type: 'json', required: false }). The type field there apparently doesn't have static typing so json was freely specified.