简体   繁体   English

Angular Typescript: Swagger CodeGen 生成器制作所有 class 属性 Nullable

[英]Angular Typescript: Swagger CodeGen Generator making all class properties Nullable

We are auto generating proxies with SwaggerCodeGen https://swagger.io/ .我们使用 SwaggerCodeGen https://swagger.io/自动生成代理。

This will reduce manually typing in hundreds of classes.这将减少手动输入数百个类。 We noticed all model class properties are Nullable?我们注意到所有 model class 属性都是可空的? in SwaggerCodeGen.在 SwaggerCodeGen 中。

When they wrote this, doesn't it break the interface contract when some members as required?当他们写这个时,当某些成员需要时,它不是破坏接口契约吗? What is the reason SwaggerCodeGen did this? SwaggerCodeGen 这样做的原因是什么? In our same C# corresponding class, they are not nullable.在我们相同的 C# 对应的 class 中,它们不可为空。

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

Currently converting C# classes to Angular Typescript 10.目前将 C# 类转换为 Angular Typescript 10。

在此处输入图像描述

Another topic:另一个话题:

Similarly in NSwag Studio,同样在 NSwag Studio 中,

They are doing this with Union Types undefined, instead of nullable.他们使用未定义的联合类型而不是可为空的来执行此操作。 Is this the same thing as Swagger CodeGen,这和 Swagger CodeGen 一样吗,

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

Swagger has a "required" property for parameters. Swagger 具有参数的“必需”属性。 If this is not set to true, the codegen will make the corresponding parameters nullable.如果未设置为 true,则 codegen 将使相应的参数可以为空。

The workaround is to customize the generator (eg type mapping) and the mustache template (eg remove check for required parameter if it's a primitive type)解决方法是自定义生成器(例如类型映射)和 mustache 模板(例如,如果它是原始类型,则删除对所需参数的检查)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular Typescript 为 class 属性动态赋值 - Angular Typescript assign values to class properties dynamically 在Angular 5 / TypeScript中获取类(接口)属性,而不分配默认值 - Get class (interface) properties in Angular 5 / TypeScript without assigning a default value 在typescript中获取类的属性 - Get properties of class in typescript Typescript 未编译 class 属性 - Typescript not compiling class properties 在Typescript中访问类的属性 - Access properties of a class in Typescript Swagger-Codegen:如何将所有文件合并到一个文件中以进行客户端代码生成 - Swagger-Codegen: How do I consolidate all files into one file for Client-Code-Generation 在打字稿类文件中获取Angular 2 Form对象以手动设置表单属性 - Get Angular 2 Form object in typescript class file to set form properties manually Swagger-codegen:错误:忽略的参数:'对象' - Swagger-codegen: error: ignored arguments: 'Object' 如何创建 Typescript 装饰器,根据以“_”开头的属性名称将所有 class 属性设置为不可枚举? - How to create Typescript decorator that sets all class properties as non-enumerable based on property name starting with “_”? 在Angular2中使用生成的Swagger TypeScript Rest Client - Using generated Swagger TypeScript Rest Client in Angular2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM