[英]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.