OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

Proposal: multi-line strings (e.g. description) in JSON

Open bajtos opened this issue 11 years ago • 14 comments

When maintaining Swagger files in the JSON format, it's rather difficult to write long multi-line strings, for example in the description field. #292 provides a good example:

{
"description": "| Header 1 | Header 2 | Header 3 |\n|----------|----------|----------|\n| Data 1   | Data 2   | Data 3   |"
}

I am proposing to support array of strings in addition to raw string in certain fields like description, notes, etc.

{
"description": [
  "| Header 1 | Header 2 | Header 3 |",
  "|----------|----------|----------|",
  "| Data 1   | Data 2   | Data 3   |"
]}

Thoughts?

/cc @shelbys @raymondfeng

bajtos avatar Aug 06 '15 15:08 bajtos

I'd say changing the JSON limitations is well beyond our scope.

webron avatar Aug 06 '15 15:08 webron

You can use YAML to have nice multi-line strings:

description: |
  | Header 1 | Header 2 | Header 3 |
  |----------|----------|----------|
  | Data 1   | Data 2   | Data 3   |

mohsen1 avatar Aug 06 '15 16:08 mohsen1

:+1:, makes things much better when hand-authoring and reviewing Specs

shelbys avatar Aug 06 '15 16:08 shelbys

Thank you for your comments. To make my proposal more clear, here is a link to commit in our fork that implements handling of multi-line text fields as I described above:

https://github.com/strongloop/strong-swagger-js/commit/6a864fb003387a3dd078aaa3d24d99bba7cdc6b4

I'd say changing the JSON limitations is well beyond our scope.

I am afraid I don't understand. I am not asking to change JSON limitations, just to provide a way how to work around them. IMO the proposed change is rather small and thus it is not adding too much extra complexity. Of course, others may have a different opinion.

You can use YAML to have nice multi-line strings:

I know about that option. In my experience, YAML is not as widely adopted and understood as JSON, therefore many projects prefer to stick to JSON (http://loopback.io/ for example, but also npmjs and most of the Node.js ecosystem).

bajtos avatar Aug 06 '15 16:08 bajtos

I don't think this will go into spec but if you want to do this write your own preprocessor that walks the specs and concatenate all description and summary fields that are arrays.

mohsen1 avatar Aug 06 '15 16:08 mohsen1

write your own preprocessor that walks the specs and concatenate all description and summary fields that are arrays

That sounds like a reasonable compromise. I am interested in viewing array descriptions using swagger-ui. Do swagger-ui and/or swagger-js provide any extensibility mechanisms where I could plug my own preprocessor? Or are you suggesting that the preprocessor should be implemented in the code producing the Swagger descriptors from JSON files?

bajtos avatar Aug 06 '15 16:08 bajtos

If it reaches to swagger-ui before being processes it's going to mark it as invalid spec.

the processing can be done anywhere before that point.

mohsen1 avatar Aug 06 '15 16:08 mohsen1

@mohsen1 - We prefer to keep the String Arrays in the Spec, because we do Code Reviews directly on the Specs, and therefore care about their readability

shelbys avatar Aug 06 '15 17:08 shelbys

@bajtos - As far as I understand, according the JSON spec (http://json.org/), strings can be Any UNICODE character except " or \ or control character. A new line is a control character and as such can't be part of the string. What you're asking is to deviate from the limitations of JSON. YAML indeed solved this issue, but that's a YAML thing, not a JSON one.

webron avatar Aug 06 '15 17:08 webron

If you try hard you can do something:

JSON.parse(`
 {"s": "multi\
        line"}
`)

but if you're looking for real readability then use YAML. I promise it's not too bad! ;)

mohsen1 avatar Aug 06 '15 17:08 mohsen1

@webron - I don't think we're talking about the same thing. To be clear we want to be able to do is the following, and just have the tools do description.join(''):

"description": [
  "Line1",
  "Line2",
  "Line3"
]

Keep in mind we have a ton of Specs spread across many teams, so switching to YAML is no trivial thing. Honestly, I prefer it personally, but that doesn't mean I can force it on everyone

shelbys avatar Aug 06 '15 17:08 shelbys

Actually, switching to YAML is extremely trivial, but that's not the point and we're definitely not looking to enforce anyone to do anything.

@shelbys - thanks for clarifying the example, I completely missed the array of the value, my apologies.

I guess we could do that, though it feels a bit hackish.

webron avatar Aug 06 '15 17:08 webron

:-1: for overloading description to support String[] so it will be readable by humans who read the JSON.

This won't actually suffice to solve this "problem", you will also need to make sure that the JSON has actual new line between array elements...

  1. Out of scope for this spec IMO - And there is already a suggestion out there (http://json5.org/)
  2. There is a better solution, YAML is much easier for people to read and edit, less error prone when editing manually, and converting between the two is effortless.
  3. Every editor supports word wrapping, and when it comes to it, this is merely a very personal choice for a terminal/code viewing setting, not something that warrants a support in this spec.

lieldulev avatar Jan 27 '16 20:01 lieldulev

Json can use \n. Yaml has native mutli-line strings. Closing, as anything else will be confusing to everyone.

fehguy avatar Feb 01 '17 20:02 fehguy