swagger-parser icon indicating copy to clipboard operation
swagger-parser copied to clipboard

Line breaks lost on bundle()

Open timothymcmackin opened this issue 5 years ago • 3 comments

I'm using swagger-parser.bundle to pull in external examples in my Swagger file via $ref. I'm noticing that in certain cases, the line breaks in the external files are lost.

For example, check out this gist, which I adapted from petstore v2: https://gist.github.com/timothymcmackin/0f9a7edd694252f38f8df1426a01c395

In my swagger, I have this $ref:

"x-code-samples": [
  {
    "lang": "shell",
    "source":{
      "$ref": "example.sh"
    }
  }
]

example.sh is a two-line file:

curl "https://petstore.swagger.io" \
--data-urlencode "petname=Roscoe"

When I transform (see resolve.js in the gist), the output document loses the line breaks:

"x-code-samples": [
  {
    "lang": "shell",
    "source": "curl \"https://petstore.swagger.io\" \\ --data-urlencode \"petname=Roscoe\""
  }
]

However, if I change example.sh to this:

curl "https://petstore.swagger.io" \
--data-urlencode "petname=Roscoe: "

the output retains the line breaks:

"x-code-samples": [
  {
    "lang": "shell",
    "source": "curl \"https://petstore.swagger.io\" \\\r\n--data-urlencode \"petname=Roscoe: \"\r\n"
  }
]

There's something about having that : in the external file that makes the line breaks stay in. Is there some bug here that's making the line breaks drop out?

Edit: I'm using swagger-parser v6.0.5.

timothymcmackin avatar Mar 21 '19 19:03 timothymcmackin

I've been over this a few times and it does not seem to have anything to do with unix vs dos line breaks in the files.

timothymcmackin avatar Apr 05 '19 19:04 timothymcmackin

Possible workaround:

  const myParser = {
    order: 1,
    parse(file) {
      return file.data.toString();
    }
  };

  const bundleOptions = {
    parse: {
      text: myParser,
    },
  };

  return new Promise((resolve) => {
    SwaggerParser.bundle(passedSourceFile, bundleOptions, (err, api) => {
      resolve(api);
    });
  });

timothymcmackin avatar Dec 27 '21 16:12 timothymcmackin

Maybe a bug here? https://github.com/APIDevTools/json-schema-ref-parser/blob/main/lib/parsers/text.js

timothymcmackin avatar Dec 27 '21 17:12 timothymcmackin