Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
fix: postman conversion not replacing variables (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Nov 12, 2022
1 parent 3189279 commit 81f9ee2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
30 changes: 15 additions & 15 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8580,7 +8580,7 @@ Contact Support:
},
"openapi": "3.0.0",
"paths": Object {
"/pet": Object {
"/v2/pet": Object {
"post": Object {
"parameters": Array [
Object {
Expand Down Expand Up @@ -8766,7 +8766,7 @@ Contact Support:
],
},
},
"/pet/findByStatus": Object {
"/v2/pet/findByStatus": Object {
"get": Object {
"description": "Multiple status values can be provided with comma separated strings",
"parameters": Array [
Expand Down Expand Up @@ -8886,7 +8886,7 @@ Contact Support:
],
},
},
"/pet/findByTags": Object {
"/v2/pet/findByTags": Object {
"get": Object {
"description": "Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.",
"parameters": Array [
Expand Down Expand Up @@ -9006,7 +9006,7 @@ Contact Support:
],
},
},
"/pet/{petId}": Object {
"/v2/pet/{petId}": Object {
"delete": Object {
"parameters": Array [
Object {
Expand Down Expand Up @@ -9283,7 +9283,7 @@ Contact Support:
],
},
},
"/pet/{petId}/uploadImage": Object {
"/v2/pet/{petId}/uploadImage": Object {
"post": Object {
"parameters": Array [
Object {
Expand Down Expand Up @@ -9370,7 +9370,7 @@ Contact Support:
],
},
},
"/store/inventory": Object {
"/v2/store/inventory": Object {
"get": Object {
"description": "Returns a map of status codes to quantities",
"parameters": Array [
Expand Down Expand Up @@ -9418,7 +9418,7 @@ Contact Support:
],
},
},
"/store/order": Object {
"/v2/store/order": Object {
"post": Object {
"parameters": Array [
Object {
Expand Down Expand Up @@ -9508,7 +9508,7 @@ Contact Support:
],
},
},
"/store/order/{orderId}": Object {
"/v2/store/order/{orderId}": Object {
"delete": Object {
"description": "For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors",
"parameters": Array [
Expand Down Expand Up @@ -9662,7 +9662,7 @@ Contact Support:
],
},
},
"/user": Object {
"/v2/user": Object {
"post": Object {
"description": "This can only be done by the logged in user.",
"parameters": Array [
Expand Down Expand Up @@ -9721,7 +9721,7 @@ Contact Support:
],
},
},
"/user/createWithArray": Object {
"/v2/user/createWithArray": Object {
"post": Object {
"parameters": Array [
Object {
Expand Down Expand Up @@ -9791,7 +9791,7 @@ Contact Support:
],
},
},
"/user/createWithList": Object {
"/v2/user/createWithList": Object {
"post": Object {
"parameters": Array [
Object {
Expand Down Expand Up @@ -9861,7 +9861,7 @@ Contact Support:
],
},
},
"/user/login": Object {
"/v2/user/login": Object {
"get": Object {
"parameters": Array [
Object {
Expand Down Expand Up @@ -9949,7 +9949,7 @@ Contact Support:
],
},
},
"/user/logout": Object {
"/v2/user/logout": Object {
"get": Object {
"responses": Object {
"500": Object {
Expand Down Expand Up @@ -9978,7 +9978,7 @@ Contact Support:
],
},
},
"/user/{username}": Object {
"/v2/user/{username}": Object {
"delete": Object {
"description": "This can only be done by the logged in user.",
"parameters": Array [
Expand Down Expand Up @@ -10222,7 +10222,7 @@ Contact Support:
},
"servers": Array [
Object {
"url": "http://{{baseurl}}",
"url": "http://petstore.swagger.io",
},
],
"tags": Array [
Expand Down
2 changes: 1 addition & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('#deref', () => {
const o = new OASNormalize(postman);
const deref = (await o.deref()) as OpenAPIV3.Document;

expect(deref?.paths?.['/pet']?.post?.requestBody).toStrictEqual({
expect(deref?.paths?.['/v2/pet']?.post?.requestBody).toStrictEqual({
content: {
'application/json': {
schema: {
Expand Down
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export default class OASNormalize {
}
}

/**
* @private
*/
static convertPostmanToOpenAPI(schema: any) {
return postmanToOpenAPI(JSON.stringify(schema), null, { outputFormat: 'json', replaceVars: true }).then(JSON.parse);
}

/**
* Bundle up the given API definition, resolving any external `$ref` pointers in the process.
*
Expand All @@ -100,7 +107,7 @@ export default class OASNormalize {
// upconvert it to an OpenAPI definition file so our returned dataset is always one of
// those for a Postman dataset.
if (utils.isPostman(schema)) {
return postmanToOpenAPI(JSON.stringify(schema), null, { outputFormat: 'json' }).then(JSON.parse);
return OASNormalize.convertPostmanToOpenAPI(schema);
}

return schema;
Expand All @@ -125,7 +132,7 @@ export default class OASNormalize {
// still upconvert it to an OpenAPI definition file so our returned dataset is always one
// of those for a Postman dataset.
if (utils.isPostman(schema)) {
return postmanToOpenAPI(JSON.stringify(schema), null, { outputFormat: 'json' }).then(JSON.parse);
return OASNormalize.convertPostmanToOpenAPI(schema);
}

return schema;
Expand Down Expand Up @@ -164,7 +171,7 @@ export default class OASNormalize {
return schema;
}

return postmanToOpenAPI(JSON.stringify(schema), null, { outputFormat: 'json' }).then(JSON.parse);
return OASNormalize.convertPostmanToOpenAPI(schema);
})
.then(async schema => {
if (!utils.isSwagger(schema) && !utils.isOpenAPI(schema)) {
Expand Down

0 comments on commit 81f9ee2

Please sign in to comment.