即使设置了 JsonSchemaFlattenAttribute,NSwag 也会生成基础 class
NSwag generates base class even though JsonSchemaFlattenAttribute is set
我正在为我的 ASP.NET Core web api 项目使用 NSwag,但无法让它工作。我想要做的是排除 IdentityUser 的一些基本属性。这是我的自定义用户 class:
[DataContract]
[JsonSchemaFlattenAttribute]
public class User : IdentityUser
{
// Overridden variables
[DataMember( Name = "id" )]
[PersonalData]
public new string Id { get; set; }
[DataMember( Name = "email" )]
[ProtectedPersonalData]
public new string Email { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new DateTimeOffset? LockoutEnd { get; set; }
[PersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new bool TwoFactorEnabled { get; set; }
[PersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new bool PhoneNumberConfirmed { get; set; }
[ProtectedPersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string PhoneNumber { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string ConcurrencyStamp { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string SecurityStamp { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string PasswordHash { get; set; }
[PersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new bool EmailConfirmed { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string NormalizedEmail { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string NormalizedUserName { get; set; }
[ProtectedPersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string UserName { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new bool LockoutEnabled { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new int AccessFailedCount { get; set; }
// Custom variables
[DataMember( Name = "date_joined_utc" )]
public DateTime DateJoinedUtc { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public virtual ICollection<RefreshToken> RefreshTokens { get; set; }
// Optimistic concurrency
[Timestamp]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public byte[] RowVersion { get; set; }
}
生成:
"User": {
"type": "object",
"required": [
"dateJoinedUtc"
],
"properties": {
"id": {
"type": "string"
},
"email": {
"type": "string"
},
"dateJoinedUtc": {
"type": "string",
"format": "date-time"
}
},
"allOf": [
{
"$ref": "#/definitions/IdentityUserOfString"
},
{}
]
},
"IdentityUserOfString": {
"type": "object",
"required": [
"emailConfirmed",
"phoneNumberConfirmed",
"twoFactorEnabled",
"lockoutEnabled",
"accessFailedCount"
],
"properties": {
"id": {
"type": "string"
},
"userName": {
"type": "string"
},
"normalizedUserName": {
"type": "string"
},
"email": {
"type": "string"
},
"normalizedEmail": {
"type": "string"
},
"emailConfirmed": {
"type": "boolean"
},
"passwordHash": {
"type": "string"
},
"securityStamp": {
"type": "string"
},
"concurrencyStamp": {
"type": "string"
},
"phoneNumber": {
"type": "string"
},
"phoneNumberConfirmed": {
"type": "boolean"
},
"twoFactorEnabled": {
"type": "boolean"
},
"lockoutEnd": {
"type": "string",
"format": "date-time"
},
"lockoutEnabled": {
"type": "boolean"
},
"accessFailedCount": {
"type": "integer",
"format": "int32"
}
}
},
我对 JsonSchemaFlattenAttribute 的理解是它应该排除基础 class 并将每个 属性 放在派生 class 中。那么为什么会生成 class IdentityUserOfString 呢?我错过了什么?
出于某种原因,在用户 class 上设置 OpenApiIgnore 解决了问题:
[DataContract]
//[JsonSchemaFlattenAttribute]
[OpenApiIgnore]
public class User : IdentityUser
生成:
"User": {
"type": "object",
"required": [
"dateJoinedUtc"
],
"properties": {
"id": {
"type": "string"
},
"email": {
"type": "string"
},
"dateJoinedUtc": {
"type": "string",
"format": "date-time"
}
}
},
我正在为我的 ASP.NET Core web api 项目使用 NSwag,但无法让它工作。我想要做的是排除 IdentityUser 的一些基本属性。这是我的自定义用户 class:
[DataContract]
[JsonSchemaFlattenAttribute]
public class User : IdentityUser
{
// Overridden variables
[DataMember( Name = "id" )]
[PersonalData]
public new string Id { get; set; }
[DataMember( Name = "email" )]
[ProtectedPersonalData]
public new string Email { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new DateTimeOffset? LockoutEnd { get; set; }
[PersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new bool TwoFactorEnabled { get; set; }
[PersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new bool PhoneNumberConfirmed { get; set; }
[ProtectedPersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string PhoneNumber { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string ConcurrencyStamp { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string SecurityStamp { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string PasswordHash { get; set; }
[PersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new bool EmailConfirmed { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string NormalizedEmail { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string NormalizedUserName { get; set; }
[ProtectedPersonalData]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new string UserName { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new bool LockoutEnabled { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public new int AccessFailedCount { get; set; }
// Custom variables
[DataMember( Name = "date_joined_utc" )]
public DateTime DateJoinedUtc { get; set; }
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public virtual ICollection<RefreshToken> RefreshTokens { get; set; }
// Optimistic concurrency
[Timestamp]
[IgnoreDataMember]
[JsonSchemaIgnoreAttribute]
public byte[] RowVersion { get; set; }
}
生成:
"User": {
"type": "object",
"required": [
"dateJoinedUtc"
],
"properties": {
"id": {
"type": "string"
},
"email": {
"type": "string"
},
"dateJoinedUtc": {
"type": "string",
"format": "date-time"
}
},
"allOf": [
{
"$ref": "#/definitions/IdentityUserOfString"
},
{}
]
},
"IdentityUserOfString": {
"type": "object",
"required": [
"emailConfirmed",
"phoneNumberConfirmed",
"twoFactorEnabled",
"lockoutEnabled",
"accessFailedCount"
],
"properties": {
"id": {
"type": "string"
},
"userName": {
"type": "string"
},
"normalizedUserName": {
"type": "string"
},
"email": {
"type": "string"
},
"normalizedEmail": {
"type": "string"
},
"emailConfirmed": {
"type": "boolean"
},
"passwordHash": {
"type": "string"
},
"securityStamp": {
"type": "string"
},
"concurrencyStamp": {
"type": "string"
},
"phoneNumber": {
"type": "string"
},
"phoneNumberConfirmed": {
"type": "boolean"
},
"twoFactorEnabled": {
"type": "boolean"
},
"lockoutEnd": {
"type": "string",
"format": "date-time"
},
"lockoutEnabled": {
"type": "boolean"
},
"accessFailedCount": {
"type": "integer",
"format": "int32"
}
}
},
我对 JsonSchemaFlattenAttribute 的理解是它应该排除基础 class 并将每个 属性 放在派生 class 中。那么为什么会生成 class IdentityUserOfString 呢?我错过了什么?
出于某种原因,在用户 class 上设置 OpenApiIgnore 解决了问题:
[DataContract]
//[JsonSchemaFlattenAttribute]
[OpenApiIgnore]
public class User : IdentityUser
生成:
"User": {
"type": "object",
"required": [
"dateJoinedUtc"
],
"properties": {
"id": {
"type": "string"
},
"email": {
"type": "string"
},
"dateJoinedUtc": {
"type": "string",
"format": "date-time"
}
}
},