Angular Typescript: Swagger CodeGen Generator making all class properties Nullable

Viewed 1528

We are auto generating proxies with SwaggerCodeGen https://swagger.io/ .

This will reduce manually typing in hundreds of classes. We noticed all model class properties are Nullable ? in SwaggerCodeGen.

When they wrote this, doesn't it break the interface contract when some members as required? What is the reason SwaggerCodeGen did this? In our same C# corresponding class, they are not nullable.

export interface Product{ 
    productId?: number;
    productName?: string;
    productType?: number;
    manufacturer?: string;
    inventory?: number;

Currently converting C# classes to Angular Typescript 10.

enter image description here

Another topic:

Similarly in NSwag Studio,

They are doing this with Union Types undefined, instead of nullable. Is this the same thing as Swagger CodeGen,

export interface Product {
    productId: number | undefined;
    productName?: string | undefined;
    productType?: number | undefined;
    manufacturer?: string | undefined;
    inventory?: number | undefined;
1 Answers

Swagger has a "required" property for parameters. If this is not set to true, the codegen will make the corresponding parameters nullable.

The workaround is to customize the generator (e.g. type mapping) and the mustache template (e.g. remove check for required parameter if it's a primitive type)

definitions:
  Order:
    required:
      - id
      - petId
    properties:
      id:
        required: true
        type: integer
        format: int64
      petId:
        type: integer
        format: int64
      quantity:
        required: true
        type: integer
        format: int32
Related