APIDevTools / swagger-parser

Swagger 2.0 and OpenAPI 3.0 parser/validator
https://apitools.dev/swagger-parser
MIT License
1.09k stars 154 forks source link

Line breaks lost on bundle() #112

Open timothymcmackin opened 5 years ago

timothymcmackin commented 5 years ago

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 commented 5 years ago

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 commented 2 years ago

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 commented 2 years ago

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