继续翻译

master
bicijinlian 4 years ago
parent 8dfbd8615b
commit 193281b363

@ -979,15 +979,18 @@ public class SecurityRequirementsOperationFilter : IOperationFilter
}
```
_NOTE: If you're using the `SwaggerUI` middleware, you can enable interactive OAuth2.0 flows that are powered by the emitted security metadata. See [Enabling OAuth2.0 Flows](#enable-oauth20-flows) for more details._
_注意:如果使用的是`SwaggerUI`中间件则可以启用由发出的安全元数据支持的交互式OAuth2.0流。有关详细信息,请参见[启用OAuth2.0流](#enable-oauth20-flows) _
### Inheritance and Polymorphism ###
### 继承与多态 ###
Swagger / OpenAPI defines the `allOf` and `oneOf` keywords for describing [inheritance and polymorphism](https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/) relationships in schema definitions. For example, if you're using a base class for models that share common properties you can use the `allOf` keyword to describe the inheritance hierarchy. Or, if your serializer supports polymorphic serializion/deserialization, you can use the `oneOf` keyword to document all the "possible" schemas for requests/responses that vary by subtype.
Swagger / OpenAPI定义了 `allOf` 和`oneOf` 关键字来描述[继承和多态性](https://swagger.io/docs/specification/data-models/inheritation-and-polymorphic/)模式定义中的关系。
例如,如果要为共享公共属性的模型使用基类,可以使用`allOf`关键字来描述继承层次结构。或者,如果序列化程序支持多态序列化/反序列化,则可以使用`oneOf`关键字为不同子类型的请求/响应记录所有"可能"的架构。
#### Enabling Inheritance ####
#### 启用继承 ####
By default, Swashbuckle flattens inheritance hierarchies. That is, for derived models, the inherited properties are combined and listed alongside the declared properties. This can cause a lot of duplication in the generated Swagger, particularly when there's multiple subtypes. It's also problematic if you're using a client generator (e.g. NSwag) and would like to maintain the inheritiance hierarchy in the generated client models. To work around this, you can apply the `UseAllOfForInheritance` setting, and this will leverage the `allOf` keyword to incorporate inherited properties by reference in the generated Swagger:
默认情况下Swashbulk会展平继承层次结构。也就是说对于派生模型继承的属性将与声明的属性一起组合并列出。这会在生成的 Swagger 中造成大量重复,特别是当有多个子类型时。
如果您使用的是客户机生成器例如NSwag并且希望在生成的客户机模型中维护继承层次结构那么也会有问题。
要解决此问题,可以应用`UseAllOfForInheritance`设置,这将利用`allOf`关键字将继承的属性通过引用合并到生成的Swagger中
```
Circle: {
@ -1015,9 +1018,9 @@ Shape: {
}
```
#### Enabling Polymorphism ####
#### 启用多态性 ####
If your serializer supports polymorphic serialization/deserialization and you would like to list the possible subtypes for an action that accepts/returns abstract base types, you can apply the `UseOneOfForPolymorphism` setting. As a result, the generated request/response schemas will reference a collection of "possible" schemas instead of just the base class schema:
如果序列化程序支持多态序列化/反序列化,并且希望列出接受/返回抽象基类型的操作的可能子类型,则可以应用 `UseOneOfForPolymorphism`设置。因此,生成的请求/响应模式将引用`可能`模式的集合,而不仅仅是基类模式:
```
requestBody: {
@ -1037,9 +1040,9 @@ requestBody: {
}
```
#### Detecting Subtypes ####
#### 检测子类型 ####
As inheritance and polymorphism relationships can often become quite complex, not just in your own models but also within the .NET class library, Swashbuckle is selective about which hierarchies it does and doesn't expose in the generated Swagger. By default, it will pick up any subtypes that are defined in the same assembly as a given base type. If you'd like to override this behavior, you can provide a custom selector function:
由于继承和多态关系通常会变得非常复杂,不仅在您自己的模型中,而且在.NET类库中Swashbuck对它在生成的swagrager中公开哪些层次结构是有选择的。默认情况下它将拾取与给定基类型在同一程序集中定义的任何子类型。如果要重写此行为可以提供自定义选择器函数
```csharp
services.AddSwaggerGen(c =>
@ -1055,13 +1058,14 @@ services.AddSwaggerGen(c =>
});
```
_NOTE: If you're using the [Swashbuckle Annotations library](#swashbuckleaspnetcoreannotations), it contains a custom selector that's based on the presence of `SwaggerSubType` attributes on base class definitions. This way, you can use simple attributes to explicitly list the inheritance and/or polymorphism relationships you want to expose. To enable this behavior, check out the [Annotations docs](#list-known-subtypes-for-inheritance-and-polymorphism)._
_注意如果您使用的是[Swashbuckle Annotations library](#swashbuckleaspnetcoreannotations),它包含一个自定义选择器,该选择器基于基类定义上是否存在`SwaggerSubType`属性。
这样,就可以使用简单的属性来显式地列出要公开的继承和/或多态关系。要启用此行为,请查看[Annotations docs](#list-known-subtypes-for-inheritance-and-polymorphism)。_
#### Describing Discriminators ####
#### 描述鉴别器 ####
In conjunction with the `oneOf` keyword, Swagger / OpenAPI also supports a `discriminator` field on polymorphic schema definitions. This keyword points to the property that identifies the specific type being represented by a given payload. In addition to the property name, the discriminator description MAY also include a `mapping` which maps discriminator values to specific schema definitions.
Swagger / OpenAPI与 `oneOf`关键字一起,还支持多态模式定义上的`discriminator` 字段。此关键字指向标识由给定负载表示的特定类型的属性。除了属性名,鉴别器描述还可以包括一个`mapping`,它将鉴别器值映射到特定的模式定义。
For example, the Newtonsoft serializer supports polymorphic serialization/deserialization by emitting/accepting a "$type" property on JSON instances. The value of this property will be the [assembly qualified type name](https://docs.microsoft.com/en-us/dotnet/api/system.type.assemblyqualifiedname?view=netcore-3.1) of the type represented by a given JSON instance. So, to explicitly describe this behavior in Swagger, the corresponding request/respose schema could be defined as follows:
例如Newtonsoft 序列化程序通过在JSON实例上发出/接受 `$type` 属性来支持多态序列化/反序列化。此属性的值将为[程序集限定类型名](https://docs.microsoft.com/en-us/dotnet/api/system.type.assemblyqualifiednameview=netcore-3.1)由给定的JSON实例表示的类型。因此为了在Swagger中显式地描述这种行为相应的请求/响应模式可以定义如下:
```
schema: {
@ -1083,9 +1087,9 @@ schema: {
}
```
If `UseOneOfForPolymorphism` is enabled, and your serializer supports (and has enabled) emitting/accepting a discriminator property, then Swashbuckle will automatically generate the corresponding `discriminator` metadata on polymorphic schema definitions.
如果启用了`UseOneOfForPolymorphism`,并且序列化程序支持(并且已经启用)发出/接受鉴别器属性那么swashbuck将自动在多态架构定义上生成相应的`discriminator` 元数据。
Alternatively, if you've customized your serializer to support polymorphic serialization/deserialization, you can provide some custom selector functions to determine the discriminator name and corresponding mapping:
或者,如果已自定义序列化程序以支持多态序列化/反序列化,则可以提供一些自定义选择器函数来确定鉴别器名称和相应的映射:
```csharp
services.AddSwaggerGen(c =>
@ -1099,7 +1103,7 @@ services.AddSwaggerGen(c =>
});
```
_NOTE: If you're using the [Swashbuckle Annotations library](#swashbuckleaspnetcoreannotations), it contains custom selector functions that are based on the presence of `SwaggerDiscriminator` and `SwaggerSubType` attributes on base class definitions. This way, you can use simple attributes to explicitly provide discriminator metadata. To enable this behavior, check out the [Annotations docs](#enrich-polymorphic-base-classes-with-discriminator-metadata)._
_注意:如果您使用的是[Swashbuckle Annotations library](#swashbuckleaspnetcoreannotations),它包含基于基类定义上 `SwaggerDiscriminator``SwaggerSubType` 属性的自定义选择器函数。这样,就可以使用简单的属性显式地提供鉴别器元数据。要启用此行为,请查看[Annotations docs](#enrich-polymorphic-base-classes-with-discriminator-metadata)_
## Swashbuckle.AspNetCore.SwaggerUI ##

Loading…
Cancel
Save