yahehe / Nancy.Swagger

Nancy plugin for generated API documentation in Swagger format.
MIT License
133 stars 60 forks source link

ResponseBuilder.Example() not outputting indented JSON #159

Open mgibson2020 opened 6 years ago

mgibson2020 commented 6 years ago

Expected Behavior

When I don't use r.Example, I get a correctly formatted string, but it does have the default values that I would like to show the user. I want it to look something like this: { "location": { "latitude": 0, "longitude": 0, "name": "string", "radius": 0 }, "options": { "groupByID": false, "groupByType": false, "makeSingleID": true, "makeSingleType": true, "site": "Center", "useFlag": true } }

Actual Behavior

If I add my example object like so r.Example("", myObject) I get the following:

{"location":{"name":"Bullseye","latitude":38,"longitude":-120,"radius":250},"options":{"makeSingleID":true,"makeSingleType":true,"groupByID":false,"groupByType":false,"groupBySameWeapon":false,"useFlag":false,"site":"Center"}}

I'm not sure if I am doing wrong but I have tried to use different forms of formatting but it continues to come out flat.

Steps to Reproduce the Problem

  1. Create an Object
  2. Add to ResponseBuilder Example(string myType, object example)

Specifications

yahehe commented 6 years ago

I'm not sure I understand. Is the problem that you're passing an object with real data in to the swagger? Because if you don't want it to have real data then just pass an object with the default values?

Is the problem with the formatting? I'm not sure why that would be a concern, json doesn't need to be formatted for readability to be well formed. If you really need to look at the direct json without using swagger-ui or similar, then your browser should be able to render it well enough. If it can't then I find http://jsonviewer.stack.hu/ comes in handy

mgibson2020 commented 6 years ago

I am trying to get into a more readable format when it displays on the page. When I don't add example at all it shows up formatted correctly but with the basic data that the user would have to change. I would like for my users to be able to copy and paste the example with some real data but also be able to see it in a good format. This is the part that shows under Response area on swagger-ui.

yahehe commented 6 years ago

Is your swagger-ui up to date? It looks like they merged this feature in last year to handle that.

mgibson2020 commented 6 years ago

Right now I have the files from dist/ in my project. Should I be doing something else as well?

One thing that I did notice, when I don't add example myself it seems to be put in an array like so [ ...Json example ]

I'm not getting then when injecting my example.

jnallard commented 6 years ago

I just tried adding a example, and it was formatted correctly.

image

The presentation of the data is handled by swagger-ui. The json blob we create doesn't use indentations at all - it's minimized. Try this url: http://petstore.swagger.io/?url=http://[YOUR_DOMAIN]/api-docs# With [YOUR_DOMAIN] being your server name and port. When I tested mine, my url was: http://petstore.swagger.io/?url=http://localhost:52232/api-docs#/Users/PostUsers

If you don't see the problem there, then you should maybe think about getting the latest version of Swagger-UI. (What version do you currently have?). If it does happen there, share a screen shot please!