2

I have an asp.net .net framework (not core) where I am using Swashbuckle to auto generate the Swagger documentation for my API.

I have also added Swashbuckle examples.

I want to use the SwaggerResponseExample to show example return data, but in the Swagger UI always has the results nested under application/json

There is an old issue on this here, but no workaround.

Is there any sort of workaround for this? I see no further updates of Swashbuckle (and hence any embedded Swagger UI).

halfer
  • 19,824
  • 17
  • 99
  • 186
peterc
  • 6,921
  • 9
  • 65
  • 131
  • There are no further updates because it's moved on to .NET Core. You should consider doing the same. – Ian Kemp Nov 11 '20 at 13:46
  • @IanKemp Yes, I do know this, and hope I can go to upcoming.net 5, but at the moment I Just can't (there is a lot of legacy backend communications to get rid of eg WCF server to server). Was just hoping for some sort of workaround, no matter how dirty. – peterc Nov 11 '20 at 23:12
  • Did you ever find a solution to this? I am also stuck in .Net framework and suddenly lost a network of examples. – Rudy Scoggins Sep 09 '21 at 13:57
  • 1
    @RudyScoggins no unfortunately, have not been able to use this feature at the moment – peterc Sep 10 '21 at 00:00
  • See my answer for how I fixed it. – Rudy Scoggins Sep 17 '21 at 14:41

1 Answers1

0

As a workaround, I discovered you can at least change what appears in the body text area for a parameter.

Within the IOperation filter that sets your example, you can search through your Operation, find your operations body data parameter that holds the model and set the parameter's "default" property value to a JSON object.

enter image description here

While this won't fix your example value on the right, this will at least provide a way to get an example in your request data.

Hopefully this helps someone stuck in .Net Framework with Swashbuckle.

Edit: Here's some vb code I use to set the data parameter:

Dim dataParameter = operation.parameters.Where(Function(z) z.name = "data").FirstOrDefault()
If dataParameter IsNot Nothing Then
  'Since upgrading to v5.6 for microsoft webAPI packages (at least that's the most likely reason), it appears to have broke request example JSON rendering on the now out of support Swashbuckle for .NET framework
  'If I set the default here, I can at least get it to load in the body text area, which is a decent workaround.
  Dim requestExample = GetRequestExample(operation.operationId)

  If requestExample Is Nothing Then Controller.Base.ThrowInternalError($"Issue with endpoint {operation.operationId}: No response example and no request example given.  Most likely, this is because you have a custom endpoint with a data parameter and did not put an example in ExamplesOperationFilter.GetRequestExample()")
  Dim requestExampleProvider = DirectCast(Activator.CreateInstance(requestExample), IProvideExamples)
  dataParameter.default = FormatAsJson(requestExampleProvider).ToString

End If
Rudy Scoggins
  • 431
  • 3
  • 8