Swagger文档中的JsonResult

时间:2018-10-03 13:06:22

标签: asp.net-mvc asp.net-web-api swagger swagger-ui swashbuckle

我正在使用Swashbuckle为我的ASP MVC Web API生成文档。除JsonResult的文档外,其他所有内容都很好。这是一个例子。我有一个测试课:

public class Test
{
    public string Testing { get; set; }
}

和控制器中的方法:

[HttpGet]
public JsonResult<Test> GetTest()
{
    Test test = new Test { Testing = "testing json" };

    return Json(test);
}

基于此配置,Swagger生成示例值:

{
    "Content": {
        "Testing": "string"
    },
    "SerializerSettings": {
        "ReferenceLoopHandling": 0,
        "MissingMemberHandling": 0,
        "ObjectCreationHandling": 0,
        "NullValueHandling": 0,
        "DefaultValueHandling": 0,
        "Converters": [{
                "CanRead": true,
                "CanWrite": true
            }
        ],
        "PreserveReferencesHandling": 0,
        "TypeNameHandling": 0,
        "MetadataPropertyHandling": 0,
        "TypeNameAssemblyFormat": 0,
        "TypeNameAssemblyFormatHandling": 0,
        "ConstructorHandling": 0,
        "ContractResolver": {},
        "EqualityComparer": {},
        "ReferenceResolver": {},
        "ReferenceResolverProvider": {
            "Method": {},
            "Target": {}
        },
        "TraceWriter": {
            "LevelFilter": 0
        },
        "Binder": {},
        "SerializationBinder": {},
        "Error": {},
        "Context": {
            "m_additionalContext": {},
            "m_state": 1
        },
        "DateFormatString": "string",
        "MaxDepth": 0,
        "Formatting": 0,
        "DateFormatHandling": 0,
        "DateTimeZoneHandling": 0,
        "DateParseHandling": 0,
        "FloatFormatHandling": 0,
        "FloatParseHandling": 0,
        "StringEscapeHandling": 0,
        "Culture": "string",
        "CheckAdditionalContent": true
    },
    "Encoding": {
        "m_codePage": 0,
        "dataItem": {
            "m_dataIndex": 0,
            "m_uiFamilyCodePage": 0,
            "m_webName": "string",
            "m_headerName": "string",
            "m_bodyName": "string",
            "m_flags": 0
        },
        "m_isReadOnly": true,
        "encoderFallback": {
            "bIsMicrosoftBestFitFallback": true
        },
        "decoderFallback": {
            "bIsMicrosoftBestFitFallback": true
        }
    },
    "Request": {}
}

哪个不正确。我不想要JsonResult的文档,而只想要我的Test类的文档。我该如何改变?如何告诉Swagger仅为Test类准备文档?

1 个答案:

答案 0 :(得分:1)

一个选择是使用SwaggerResponse看一下这个示例:

[SwaggerResponse(400, "Bad request")]
public class SwaggerAnnotatedController : ApiController
{
    [SwaggerResponseRemoveDefaults]
    [SwaggerResponse(HttpStatusCode.Created, Type = typeof(int))]
    [SwaggerResponse(HttpStatusCode.BadRequest, "Invalid message", typeof(HttpError))] 
    public int Create(Message message)
    {
        throw new NotImplementedException();
    }

这是项目的链接:

https://github.com/domaindrivendev/Swashbuckle/blob/5489aca0d2dd7946f5569341f621f581720d4634/Swashbuckle.Dummy.Core/Controllers/SwaggerAnnotatedController.cs#L15