5

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;
2
  • 1
    Hi @AlanSmith, can you share the content of your swagger.json? That's the basis of everything being generated Commented Jul 21, 2020 at 9:28
  • I think a first step to figuring out where to look for a solution would be to manually update the swagger file so that nullable is set to false. See if that generates the TS how you would expect it. I would also try @monkey-0001 answer as well. If that all checks out, then you need to figure out how your api language need to look like so that it gets generated correctly (ie: in c# you might need the [Required] attribute on your model property). Commented Jul 22, 2020 at 23:12

1 Answer 1

2
+25

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.