- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 .Net 5.0 Web API 中使用 Swashbuckle (6.1.1)。我仍在学习,但我想实现一个类,其中某些属性仅在使用 GET
“阅读”时才有效。 , 以及使用 POST
“写入”时的其他属性.根据OpenAPI spec :
You can use the readOnly and writeOnly keywords to mark specific properties as read-only or write-only. This is useful, for example, when GET returns more properties than used in POST – you can use the same schema in both GET and POST and mark the extra properties as readOnly. readOnly properties are included in responses but not in requests, and writeOnly properties may be sent in requests but not in responses.
readOnly
生成 OpenAPI 规范。和
writeOnly
关键作品。
public class testDetails
{
public string commonProperty { get; set; }
public string readOnlyProperty { get; set; }
public string writeOnlyProperty { get; set; }
}
[ProducesResponseType(StatusCodes.Status200OK)]
[HttpGet("Get")]
public IActionResult Get([FromQuery] testDetails details)
{
Debug.WriteLine($"commonProperty is {details.commonProperty}");
Debug.WriteLine($"readOnlyProperty is {details.readOnlyProperty}");
Debug.WriteLine($"writeOnlyProperty is {details.writeOnlyProperty}");
return Ok();
}
我要
readOnlyProperty
被标记为
readOnly
, 和
writeOnlyProperty
被标记为
writeOnly
在生成的
swagger.json
.
writeOnlyProperty
不应作为任何
GET
的属性出现(但会出现在
POST
/
PUT
中),相反
readOnlyProperty
应该可用于
GET
但不是
POST
.
System.ComponentModel
[ReadOnly
] 属性,但没有效果。我也尝试将访问器更改为
public class testDetails
{
public string commonProperty { get; set; }
public string readOnlyProperty { get; internal set; }
public string writeOnlyProperty { internal get; set; }
}
...但这最终只会完全隐藏属性。这些都不会影响代码的实际操作,但我仍然希望属性只能在需要的地方写入,否则为只读 - 正如 OpenAPI 规范所描述的那样。有没有办法做到这一点,而无需创建单独的“读写类”?
最佳答案
您可以使用 SwaggerSchemaAttribute 注释只读和只写属性来自包裹Swashbuckle.AspNetCore.Annotations
.这将允许您使用 readOnly
生成 OpenAPI 规范。和 writeOnly
关键字并从 Swagger UI 隐藏属性。
跟着这些步骤:
Install-Package Swashbuckle.AspNetCore.Annotations
ConfigureServices
Startup.cs
的方法, 在 Swagger 配置块中启用注释:services.AddSwaggerGen(c =>
{
...
c.EnableAnnotations();
});
public class TestDetails
{
public string CommonProperty { get; set; }
[SwaggerSchema(ReadOnly = true)]
public string ReadOnlyProperty { get; set; }
[SwaggerSchema(WriteOnly = true)]
public string WriteOnlyProperty { get; set; }
}
您的 Controller 可能如下所示:
[ApiController]
[Route("[controller]")]
public class DataController : ControllerBase
{
private TestDetails testDetails = new TestDetails()
{
CommonProperty = "Common prop value",
ReadOnlyProperty = "ReadOnly prop value",
WriteOnlyProperty = "WriteOnly prop value"
};
public DataController()
{
}
[HttpGet]
[ProducesResponseType(typeof(TestDetails), (int) HttpStatusCode.OK)]
public IActionResult Get()
{
return Ok(testDetails);
}
[HttpPost]
[ProducesResponseType((int)HttpStatusCode.OK)]
public IActionResult Post([FromBody] TestDetails details)
{
return Ok();
}
}
关于asp.net-web-api - 如何使用 Swashbuckle 实现 OpenAPI readOnly 和 writeOnly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66992487/
我们即将为我的客户端实现一个服务 API,它由许多服务组成,比如说 ServiceA、ServiceB 和 ServiceC。每个服务都可以随着时间的推移(独立地)引入新版本,而旧版本仍然存在。所以我
我正在使用 OpenAPI 3 并有两个查询参数,其中至少一个是必需的,但哪个无关紧要。 即,作为sudocode: if parameter_1 OR parameter_2: do stu
为了生成客户端库,我会在构建时获取 Quarkus 生成的 openapi yml 文件。 目前我发现获取它的唯一方法是运行服务器并从/q/openapi 端点获取它,但在这个过程中必须运行服务器只是
假设我有一个描述 API Foo 的 OpenAPI 3 文档,如下所示: openapi: 3.0.0 info: version: '1' title: Foo paths: /foo:
我正在使用 FastAPI,它允许 pattern=re.compile("(?P[42a-z]+)...") . https://editor.swagger.io/显示此模式的错误。 我的猜测是
我有一个 Spring boot Gradle 项目,我想获取它的 OpenAPI 规范 YAML 文件。 据我了解官方swagger-core不支持 Spring boot 项目,所以我找到了 sp
我正在尝试记录包含各种身份验证是可选的端点的现有 API。也就是说,如果用户获得授权,则返回的数据比未授权时返回的数据多。 无法在 OAspec v3 中明确找到。是否有编码技巧来定义这种情况? 我目
Here它说我可以引用另一个文件中单个路径的定义,但该示例似乎引用了整个文件,而不是 paths 下的单个路径定义。目的。如何在另一个文件的 paths 中分配单个路径目的? 例如,我有 Anothe
我在 SwaggerHub 注册并使用 OpenAPI 3.0 创建了一个新 API。在我的 API 中,/tasks path 有 2 个非必需参数,但我无法将它们设置为不需要 - 编辑器显示“不允
在 OpenAPI 3.0 Specification ,根OpenAPI Object有 servers属性是 Server Objects 的数组.和 Path Item Object还允许可选的
我想要做的是指定有时对 API 调用的响应可能是 PDF 文档,有时是 JSON。我想以 OpenAPI 3.0 格式执行此操作。对于 PDF,响应将如下所示: responses:
我正在编写一个返回 MP3 文件的 API YAML。我不熟悉多媒体响应。在通过 Google 时,我发现我可以使用 audio/mp3 内容类型。但我找不到任何例子来说明如何去做。我应该如何处理这种
我正在寻找一个 JS 库(最好能在浏览器中使用)来: 根据 OpenAPI 3.0 架构(YAML 或 JSON)检查特定的 JSON 负载(通常是来自 API 的响应) 使用从 OpenAPI 3.
我正在构建一个简单的 OpenAPI 3 YAML 规范,如下所示: paths: /query: get: parameters: - $ref: '#/co
与 draft-07 相比,它定义了: { "type": ["object", "boolean"], "properties": { ... "$r
我正在尝试使用 OpenAPI 3、YAML 来构建 API 定义。 我一直在使用文档 https://swagger.io/docs/specification/adding-examples/关于
我有一个 Open API 3 规范的 yaml 文件,它有一些 x- 前缀的属性。我正在尝试使用 openapi-generator-cli 生成一个 Angular Typescript SDK。
我目前正在按照 Oracle 本身的指南和教程构建基于 Helidon Microprofile 的微服务,但在使用注释时遇到了与“自动 OpenAPI 规范生成器”相关的问题。 我的 POM 包含
我必须从头开始创建一个rest api。通过手动完成大部分工作,我已经对 Jersey 有了一些经验。 我想现在就做,因为这个项目是新的。因此,我目前正在尝试每次尝试 openapi 3.0 在线编辑
我想将以下 JSON 表示为 schema在 OpenAPI 3.0 API 定义中: { get-question: { question-id:string } } 到目前为止,我已经写了
我是一名优秀的程序员,十分优秀!