-1

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.

However when the JSON schema is constructed and returned the string also contains escape character backslashes. For example the normally correct string of

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

Will be returned as the following in 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.

Is there anyway I am able to change the response type of the Swagger endpoint so that this doesn't occur? There is a dropdown available in the UI that allows me to change the response type, but only application/json is available.

Swashbuckle.AspNetCore and Swashbuckle.AspNetCore.Swagger versions 3.0.0 are being used.

Chris.ZA
  • 1,218
  • 1
  • 15
  • 30
Jake12342134
  • 1,539
  • 1
  • 18
  • 45
  • How are you viewing that response? Are you sure its not just how you are viewing it that is adding those `\` characters? eg if you are looking at it in the debugger view in visual studio then you will see those characters escaped even though those escape sequences are not present in the original string... – Chris Nov 12 '18 at 14:04

1 Answers1

2

Are you returning a generic type for all api endpoint? Since you are using .Net Core why not use ProducesResponseType attribute and mention the type to return like

[ProducesResponseType(typeof(Your_Type))]
Rahul
  • 76,197
  • 13
  • 71
  • 125