简体   繁体   English

如何更改Swagger响应的类型?

[英]How do I change the type of a Swagger response?

I have a swagger API constructed that uses a POST endpoint to take in a JSON file and returns a valid JSON schema for that file. 我构造了一个醒目的API,该API使用POST端点接收JSON文件并为该文件返回有效的JSON模式。

However when the JSON schema is constructed and returned the string also contains escape character backslashes. 但是,当构造并返回JSON模式时,字符串还包含转义字符反斜杠。 For example the normally correct string of 例如,通常正确的字符串

{"$schema": "http://json-schema.org/draft-04/schema#"}

Will be returned as the following in Swagger API: 在Swagger API中将以以下形式返回:

"{\"$schema\": \"http://json-schema.org/draft-04/schema#\"}

As you can see the backslashes that are included should not be there. 如您所见,包含的反斜杠不应该存在。 The backslashes are used to escape the " character within C#, however when this data is returned as JSON it seems the escape characters are also being printed instead of parsed and removed. 反斜杠用于在C#中转义"字符,但是当此数据作为JSON返回时,似乎转义字符也正在打印而不是解析和删除。

Is there anyway I am able to change the response type of the Swagger endpoint so that this doesn't occur? 无论如何,我是否能够更改Swagger端点的响应类型,以免发生这种情况? There is a dropdown available in the UI that allows me to change the response type, but only application/json is available. UI中有一个下拉菜单,允许我更改响应类型,但只有application/json可用。

Swashbuckle.AspNetCore and Swashbuckle.AspNetCore.Swagger versions 3.0.0 are being used. 使用的Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Swagger版本3.0.0。

Are you returning a generic type for all api endpoint? 您是否为所有api端点返回通用类型? Since you are using .Net Core why not use ProducesResponseType attribute and mention the type to return like 由于您使用的是.Net Core,为什么不使用ProducesResponseType属性并提及要返回的类型,例如

[ProducesResponseType(typeof(Your_Type))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM