Skip to content

Commit 9a827ab

Browse files
authored
fix(openapi/upload): handle funky URLs (#1344)
| 🚥 Resolves RM-13895 | | :------------------- | ## 🧰 Changes fixes a few issues where URLs with no path (e.g., `https://example.com`) or URLs with query params (e.g., `https://example.com/openapi?something=true`) weren't being handled properly. ## 🧬 QA & Testing do tests pass?
1 parent b422004 commit 9a827ab

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

__tests__/commands/openapi/__snapshots__/upload.test.ts.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,36 @@ exports[`rdme openapi upload > given that the API definition is a URL > and the
8080
}
8181
`;
8282
83+
exports[`rdme openapi upload > given that the API definition is a URL > and the \`--slug\` flag is passed > should create a new API definition in ReadMe with the specified slug where URL has no path 1`] = `
84+
{
85+
"result": {
86+
"status": "done",
87+
"uri": "/branches/1.0.0/apis/custom-slug.json.json",
88+
},
89+
"stderr": "- Validating the API definition located at https://example.com...
90+
- Creating your API definition to ReadMe...
91+
Creating your API definition to ReadMe... done!
92+
",
93+
"stdout": "🚀 Your API definition (custom-slug.json) was successfully created in ReadMe!
94+
",
95+
}
96+
`;
97+
98+
exports[`rdme openapi upload > given that the API definition is a URL > and the \`--slug\` flag is passed > should create a new API definition in ReadMe with the specified slug where URL is a query param 1`] = `
99+
{
100+
"result": {
101+
"status": "done",
102+
"uri": "/branches/1.0.0/apis/custom-slug.json.json",
103+
},
104+
"stderr": "- Validating the API definition located at https://example.com/openapi.json?somequery=true...
105+
- Creating your API definition to ReadMe...
106+
Creating your API definition to ReadMe... done!
107+
",
108+
"stdout": "🚀 Your API definition (custom-slug.json) was successfully created in ReadMe!
109+
",
110+
}
111+
`;
112+
83113
exports[`rdme openapi upload > given that the API definition is a URL > should create a new API definition in ReadMe 1`] = `
84114
{
85115
"result": {

__tests__/commands/openapi/upload.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,64 @@ describe('rdme openapi upload', () => {
612612
fileMock.done();
613613
mock.done();
614614
});
615+
616+
it('should create a new API definition in ReadMe with the specified slug where URL is a query param', async () => {
617+
const customFilename = 'custom-slug.json';
618+
const fileMock = nock('https://example.com').get('/openapi.json?somequery=true').reply(200, petstore);
619+
620+
const mock = getAPIv2Mock({ authorization: `Bearer ${key}` })
621+
.get(`/branches/${branch}/apis`)
622+
.reply(200, {})
623+
.post(`/branches/${branch}/apis`, body =>
624+
body.match(`form-data; name="schema"; filename="${customFilename}"`),
625+
)
626+
.reply(200, {
627+
data: {
628+
upload: { status: 'done' },
629+
uri: `/branches/${branch}/apis/${customFilename}.json`,
630+
},
631+
});
632+
633+
const result = await run([
634+
'--branch',
635+
branch,
636+
`${fileUrl}?somequery=true`,
637+
'--key',
638+
key,
639+
'--slug',
640+
'custom-slug',
641+
]);
642+
643+
expect(result).toMatchSnapshot();
644+
645+
fileMock.done();
646+
mock.done();
647+
});
648+
649+
it('should create a new API definition in ReadMe with the specified slug where URL has no path', async () => {
650+
const customFilename = 'custom-slug.json';
651+
const fileMock = nock('https://example.com').get('/').reply(200, petstore);
652+
653+
const mock = getAPIv2Mock({ authorization: `Bearer ${key}` })
654+
.get(`/branches/${branch}/apis`)
655+
.reply(200, {})
656+
.post(`/branches/${branch}/apis`, body =>
657+
body.match(`form-data; name="schema"; filename="${customFilename}"`),
658+
)
659+
.reply(200, {
660+
data: {
661+
upload: { status: 'done' },
662+
uri: `/branches/${branch}/apis/${customFilename}.json`,
663+
},
664+
});
665+
666+
const result = await run(['--branch', branch, `https://example.com`, '--key', key, '--slug', 'custom-slug']);
667+
668+
expect(result).toMatchSnapshot();
669+
670+
fileMock.done();
671+
mock.done();
672+
});
615673
});
616674
});
617675

src/commands/openapi/upload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export default class OpenAPIUploadCommand extends BaseCommand<typeof OpenAPIUplo
128128

129129
const specFileTypeIsUrl = specFileType === 'url';
130130

131-
let filename = specFileTypeIsUrl ? nodePath.basename(specPath) : slugify.default(specPath);
131+
let filename = specFileTypeIsUrl ? nodePath.basename(new URL(specPath).pathname) : slugify.default(specPath);
132132

133133
// Our support for Postman collections relies on an internal fork of an archived project and
134134
// can be subject to quirks in the conversion. We should let users know that this is all very
@@ -157,7 +157,7 @@ export default class OpenAPIUploadCommand extends BaseCommand<typeof OpenAPIUplo
157157
*/
158158
let updateLegacyIdViaSlug = false;
159159

160-
const fileExtension = nodePath.extname(filename);
160+
const fileExtension = nodePath.extname(filename) || '.json';
161161
if (this.flags.slug) {
162162
// verify that the slug's extension matches the file's extension
163163
const slugExtension = nodePath.extname(this.flags.slug);

0 commit comments

Comments
 (0)