swagger-js
swagger-js copied to clipboard
XML response will be parsed wrong by yaml parser if it has semicolon
Q&A (please complete the following information)
- OS: Linux
- Environment: 22.11.0
- Method of installation: npm
- Swagger-Client version: 3.34.3
- Swagger/OpenAPI version: OpenApi 3.0.2 (Petstore)
Content & configuration
If the response type is a xml file (application/xml) and it has at least a semicolon (":") somewhere, the yaml parser changes the output or if it has multiple it would throw an error.
That's because even if the type is a string or xml it has to go through the yaml parser.
My suggestion is that if the type is not json or yaml, it shouldn't be tried to be parsed and be put directly into body.
Swagger/OpenAPI definition:
https://petstore3.swagger.io/api/v3/openapi.json
Swagger-Client usage:
const SwaggerClient = require('swagger-client')
SwaggerClient({ url: 'https://petstore3.swagger.io/api/v3/openapi.json' })
.then(client => client.execute({
operationId: 'getPetById',
responseContentType: 'application/xml',
parameters: { petId: 66 }
})
.then(response => {
console.log(response)
})
)
Describe the bug you're encountering
The response will return a wrong body value or adds an parse yaml error (which makes no sense if I expect a xml or text file)
reponse with 1 semicolon: added a quote (separated before and after semicolon)
{
ok: true,
url: 'https://petstore3.swagger.io/api/v3/pet/66',
status: 200,
statusText: 'OK',
headers: {
'content-type': 'application/xml',
....
},
text: '<Pet><id>66</id><category><id>0</id><name/></category><name>Krawallnudel: Lilli</name><photoUrls/><tags/><status>available</status></Pet>',
data: '...',
// please notice the quote which was added after Krawallnudel and before Lilli
body: {
'<Pet><id>66</id><category><id>0</id><name/></category><name>Krawallnudel': 'Lilli</name><photoUrls/><tags/><status>available</status></Pet>'
},
obj: { '...' }
}
response with 2 semicolons: parse error
{
ok: true,
url: 'https://petstore3.swagger.io/api/v3/pet/66',
status: 200,
statusText: 'OK',
headers: {
'content-type': 'application/xml',
...
},
text: '<Pet><id>66</id><category><id>0</id><name/></category><name>Krawallnudel: Lilli: the dog</name><photoUrls/><tags/><status>available</status></Pet>',
data: '<Pet><id>66</id><category><id>0</id><name/></category><name>Krawallnudel: Lilli: the dog</name><photoUrls/><tags/><status>available</status></Pet>',
parseError: YAMLException: bad indentation of a mapping entry (1:80)
1 | ... egory><name>Krawallnudel: Lilli: the dog</name><photoUrls/><tag ...
-----------------------------------------^
at generateError (/workspace/openapi-red/node_modules/js-yaml/lib/loader.js:183:10)
at throwError (/workspace/openapi-red/node_modules/js-yaml/lib/loader.js:187:9)
at readBlockMapping (/workspace/openapi-red/node_modules/js-yaml/lib/loader.js:1182:7)
at composeNode (/workspace/openapi-red/node_modules/js-yaml/lib/loader.js:1441:12)
at readDocument (/workspace/openapi-red/node_modules/js-yaml/lib/loader.js:1625:3)
at loadDocuments (/workspace/openapi-red/node_modules/js-yaml/lib/loader.js:1688:5)
at Object.load (/workspace/openapi-red/node_modules/js-yaml/lib/loader.js:1714:19)
at parseBody (/workspace/openapi-red/node_modules/swagger-client/lib/http/serializers/response/index.js:15:26)
at /workspace/openapi-red/node_modules/swagger-client/lib/http/serializers/response/index.js:54:21
at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
reason: 'bad indentation of a mapping entry',
mark: {
name: null,
buffer: '<Pet><id>66</id><category><id>0</id><name/></category><name>Krawallnudel: Lilli: the dog</name><photoUrls/><tags/><status>available</status></Pet>\n',
position: 79,
line: 0,
column: 79,
snippet: ' 1 | ... egory><name>Krawallnudel: Lilli: the dog</name><photoUrls/><tag ...\n' +
'-----------------------------------------^'
}
}
}
To reproduce...
Steps to reproduce the behavior:
- Add a pet in petstore with one or multiple semicolons somewhere in a string
- get the pet with getPetById and response type "application/xml"
Expected behavior
Body and Text should have the same value as text