我正在使用Autorest
从 swagger 2.0 定义生成 csharp sdk,我想知道是否可以让生成的子类覆盖具有不同类型的父属性。
大摇大摆的例子:
{
"swagger": "2.0",
"info": {
"version": "1",
"title": "My API"
},
"schemes": [
"https"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/myResource": {
"post": {
"parameters": [
{
"name": "myResource_data",
"in": "body",
"schema": {
"$ref": "#/definitions/myResourceCreateBody"
},
"required": true
}
],
"responses": {
"201": {
"description": "myResource response",
"schema": {
"$ref": "#/definitions/myResourceResponseExtended"
}
}
}
}
}
},
"definitions": {
"metaResponse": {
"type": "object",
"description": "Response metadata.",
"required": [
"api_version"
],
"properties": {
"api_version": {
"type": "string"
}
},
"x-services": [
"shared"
]
},
"extendedMetaResponse": {
"allOf": [
{
"$ref": "#/definitions/metaResponse"
},
{
"type": "object",
"properties": {
"myExtendedProp": {
"type": "string"
}
}
}
],
"x-services": [
"shared"
]
},
"myResourceResponse": {
"type": "object",
"required": [
"meta"
],
"additionalProperties": false,
"properties": {
"meta": {
"$ref": "#/definitions/metaResponse"
}
},
"x-services": [
"myResource"
]
},
"myResourceResponseExtended": {
"allOf": [
{
"$ref": "#/definitions/myResourceResponse"
},
{
"type": "object",
"properties": {
"meta": {
"$ref": "#/definitions/extendedMetaResponse"
}
}
}
],
"x-services": [
"myResource"
]
},
"myResourceCreateBody": {
"type": "object",
"required": [
"myResource_id"
],
"additionalProperties": false,
"properties": {
"myResource_id": {
"type": "string",
"description": "myResource identifier"
}
},
"x-services": [
"myResource"
]
}
}
}
它生成以下 C# 基类:
public partial class MyResourceResponse
{
public MyResourceResponse()
{
CustomInit();
}
public MyResourceResponse(MetaResponse meta)
{
Meta = meta;
CustomInit();
}
partial void CustomInit();
[JsonProperty(PropertyName = "meta")]
public MetaResponse Meta { get; set; }
public virtual void Validate()
{
if (Meta == null)
{
throw new ValidationException(ValidationRules.CannotBeNull, "Meta");
}
if (Meta != null)
{
Meta.Validate();
}
}
}
}
以及以下子类:
public partial class MyResourceResponseExtended : MyResourceResponse
{
public MyResourceResponseExtended()
{
CustomInit();
}
public MyResourceResponseExtended(MetaResponse meta)
: base(meta)
{
CustomInit();
}
partial void CustomInit();
public override void Validate()
{
base.Validate();
}
}
但我想要的输出是必须用类型属性MyResourceResponseExtended
覆盖该Meta
属性以公开其附加属性。Meta
MetaResponseExtended
例如,我想要的输出是:
public partial class MyResourceResponseExtended : MyResourceResponse
{
[JsonProperty(PropertyName = "meta")]
public new ExtendedMetaResponse Meta { get; set; }
public MyResourceResponseExtended(ExtendedMetaResponse meta)
: base(meta)
{
Meta = meta;
CustomInit();
}
}
在 Swagger 2.0 中是否有更好的方法来定义它?这是 Autorest 的错误/限制吗?
谢谢!:-)
PS:我已经研究discriminator
过在 swagger 文件中使用,但我要么没有正确使用它,要么它不是为这个特定目的而设计的。