Swagger UI customize response body

24 views Asked by At

I have a question regarding to customize response body in swaggerUI. Let's say I have an endpoint: endpoint

where example value for 'localDate' looks like:

{
"localDate": "Wednesday, 13 March 2024"
}

But when I try it out in response I can find whole object in response:

{
  "localDate": {
    "calendar": {
      "id": "ISO",
      "name": "ISO",
      "minYear": -9998,
      "maxYear": 9999,
      "eras": [
        {
          "name": "BCE"
        },
        {
          "name": "CE"
        }
      ]
    },
    "year": 2024,
    "month": 3,
    "day": 14,
    "dayOfWeek": 4,
    "yearOfEra": 2024,
    "era": {
      "name": "CE"
    },
    "dayOfYear": 74
  }
}

My endpoint:

[HttpGet("any-simple-random-object")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<actionresult<anysimpleobjectresponse>> GetAnySimpleObject()
=> await Task.FromResult(AnySimpleObjectResponse.CreateRandomObject());

My response generator:

public record AnySimpleObjectResponse(LocalDate LocalDate)
{
public static AnySimpleObjectResponse CreateRandomObject()
=> new(LocalDate.FromDateTime(DateTime.Now));
}

The question is, what should I do to have response displayed only in swagger UI like in example (e.g. for postman I want to see whole object).

I'm looking for general mechanism, I don't want to add any attributes for every potential response object or for all endpoints.

0

There are 0 answers