openapi-typescript-codegen
openapi-typescript-codegen copied to clipboard
Apparently there is no option to generate non nullable model properties
Hello, is there a way to generate non nullable model properties? Whatever I do it always generates as nullable.
// Current
export type SampleModel= {
title?: string;
url?: string;
};
// Expected
export type SampleModel= {
title: string;
url: string;
};
In my swagger json I also added these properties as required but I got the same result.
I'm using this command
npx openapi-typescript-codegen --input $swaggerUrl --output ./src/interfaces/--exportCore=false --exportServices=false
Yes, instead of:
SampleModel:
type: object
properties:
title:
type: string
url:
type: string
Use:
SampleModel:
required:
- title
- url
type: object
properties:
title:
type: string
url:
type: string
See the spec for more info.
I also was a bit curious about what is correct here
so basically string? in typescript does not mean nullable but optional, then it makes sense
nullable in typescript would be string | null
This is the same issue as https://github.com/ferdikoomen/openapi-typescript-codegen/issues/1300. The solution is to change the schema. If using NSwag have a look at https://github.com/RicoSuter/NSwag/issues/3110#issuecomment-799519820.
But how do I make C# property:
[Required]
public string? PaymentMethodId { get; set; }
to generatea:
paymentMethodId: string | null;
Adding attribute required always makes it non-nullable. Removing it alwats make it optional and nullable doesn't matter if it's optional in C#
Any suggestions?