Peska
Peska

Reputation: 4140

JsonResult in Swagger documentation

I'm using Swashbuckle to generate documentation for my ASP MVC Web API. Everything works great, except documentation to JsonResult. Here is an example. I have a test class:

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

and method in controller:

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

    return Json(test);
}

Based on this configuration, Swagger generates example value:

{
    "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": {}
}

Which is incorrect. I don't want a documentation for JsonResult, but only for my Test class. How can I change that? How can I tell Swagger to prepare documentation only for Test class?

Upvotes: 1

Views: 1279

Answers (1)

Helder Sepulveda
Helder Sepulveda

Reputation: 17664

An option is be to use SwaggerResponse take a look at this sample:

[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();
    }

Here is a link to the project:

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

Upvotes: 1

Related Questions