[英].net Core & Swashbuckle/Swagger: How to provide raw json example?
I have a WebAPI controller with an operation returning a JSON schema.我有一个 WebAPI controller,其操作返回 JSON 架构。 This JSON return value cannot be created by serializiation, so I designed the operation method as follow:这个JSON的返回值是不能序列化创建的,所以我设计了如下操作方法:
[HttpGet("{serviceName}/contract")]
[SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(object))]
public IActionResult GetContract(string serviceName)
{
return Content("{ \"type\": \"object\" }", "application/json"); // for example ...
}
Now I like to have a or some documented return values for Swagger. But I'm unable to do that.现在我想要 Swagger 的一个或一些记录的返回值。但我无法做到这一点。 There is the SwaggerRequestExample
attribute, but as said before, this requires a return type which in my case is not applicable.有SwaggerRequestExample
属性,但如前所述,这需要一个返回类型,这在我的情况下不适用。
Basically I search for a way of something like that (just dynamic):基本上我寻找一种类似的方式(只是动态的):
[SwaggerResponseExample((int)HttpStatusCode.OK, "{\"anyJson\": \"Yes, I am!\"}")]
Or of course, even better like that:或者当然,更好的是:
[SwaggerResponseExample((int)HttpStatusCode.OK, RawJsonFabricType="TypeName", RawJsonStaticMethod="MethodName")]
Use case: The JSON schemas I need to return in operation method are stored in a database and are not created within the program code itself.用例:我需要在操作方法中返回的 JSON 模式存储在数据库中,而不是在程序代码本身中创建的。
A concrete example of such a JSON schema value is:这种 JSON 模式值的具体示例是:
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}
Help will be very appreciated.帮助将不胜感激。 Thanks!谢谢!
I'm using c#.net core 6.我正在使用 c#.net 核心 6。
Afer trying arround I came to the solution:在尝试之后我找到了解决方案:
First: Add ExampleProvider
and use generic type JsonDocument
(from System.Text.Json
):第一:添加ExampleProvider
并使用通用类型JsonDocument
(来自System.Text.Json
):
public class ServiceDemandContractExampleProvider : IExamplesProvider<JsonDocument>
{
/// <inheritdoc/>
public JsonDocument GetExamples()
{
var jsonToShow = JsonDocument.Parse(@"{
""$id"": ""https://example.com/person.schema.json"",
""$schema"": ""https://json-schema.org/draft/2020-12/schema"",
""title"": ""Person"",
""type"": ""object"",
""properties"": {
""firstName"": {
""type"": ""string"",
""description"": ""The person's first name.""
},
""lastName"": {
""type"": ""string"",
""description"": ""The person's last name.""
},
""age"": {
""description"": ""Age in years which must be equal to or greater than zero."",
""type"": ""integer"",
""minimum"": 0
}
}
}");
return jsonToShow;
}
}
To JsonDocument.Parse
put whatever JSON (in my case loaded content from database).在JsonDocument.Parse
中放置任何 JSON(在我的例子中是从数据库加载的内容)。
Then add the follow attributes to the operation method:然后在操作方法中添加如下属性:
[SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(object))]
[SwaggerResponseExample((int)HttpStatusCode.OK, typeof(ServiceDemandContractExampleProvider))]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.