Currently using 3.0.3
of the OpenAPI spec, because that's that latest that version 6.0.0
of openapi-generator supports at this time.
We generate typescript
for the app browser client and java
for the server.
Current OpenAPI spec, that generates a status
field of type string:
UpdateAuthzResponse:
description: >
'APPROVED' or 'REQUESTED'
type: object
required: [status]
properties:
status: {type: string}
For typescript
this currently generates something like:
export interface UpdateAuthzResponse {
status: string;
}
Is there an OpenAPI 3.0.0 spec that would generate a Typescript union using the openapi-generator? Something like:
export interface UpdateAuthzResponse {
status: 'APPROVED' | 'REQUESTED';
}
I guess it would just generate an enum
on java
side.
I'm not looking to change the tool we use on the typescript
side at the moment. We may do that later, but this question is specifically about openapi-generator
.
As posted in answer by dcstraw
, the openapi should look like this (I have modelled the status as a separate object):
components:
schemas:
AuthzRequestStatus:
type: string
enum: ['APPROVED', 'REQUESTED', 'REJECTED']
But the stringEnums
flag (regardless of value being "true"
or "false"
) was causing openapi-generator
to generate code like:
export enum AuthzRequestStatus {
Approved = 'APPROVED',
Requested = 'REQUESTED',
Rejected = 'REJECTED'
}
When I removed the stringEnums
flag entirely from my configuration, openapi-generator generated this:
export const AuthzRequestStatus = {
Approved: 'APPROVED',
Requested: 'REQUESTED',
Rejected: 'REJECTED'
} as const;
export type AuthzRequestStatus = typeof AuthzRequestStatus[keyof typeof AuthzRequestStatus];
Which is roughly what I wanted (I expected it to just generate a union type directly, but building the union off the keys of the enum works fine for me, so good enough).
openapi-generator config can be found here if interested: https://github.com/au-research/raido-v2/blob/475adbdaf2f78095dbe30ddbb1c3cc3e6511a048/app-client/build.gradle#L26
Usage of the generated type: https://github.com/au-research/raido-v2/blob/475adbdaf2f78095dbe30ddbb1c3cc3e6511a048/app-client/src/Page/Admin/AuthzRespondPage.tsx#L119