Skip to content

Commit af9217d

Browse files
committed
Fixed issue where defintions with empty title were converted with empty collection name
1 parent a9d2b9b commit af9217d

File tree

5 files changed

+218
-8
lines changed

5 files changed

+218
-8
lines changed

lib/schemapack.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
// This is the default collection name if one can't be inferred from the OpenAPI spec
4-
const COLLECTION_NAME = 'Imported from OpenAPI 3.0',
4+
const COLLECTION_NAME = 'Imported from OpenAPI',
55
{ getConcreteSchemaUtils } = require('./common/versionUtils.js'),
66
{ convertToOAS30IfSwagger } = require('./swaggerUtils/swaggerToOpenapi.js'),
77
BROWSER = 'browser',
@@ -194,12 +194,18 @@ class SchemaPack {
194194
}
195195
// if the schema is validated, return the meta data as required.
196196
else if (this.validated) {
197+
let name = _.get(this.openapi, 'info.title');
198+
199+
if (_.isEmpty(name) || !_.isString(name)) {
200+
name = COLLECTION_NAME;
201+
}
202+
197203
return cb(null, {
198204
result: true,
199-
name: _.get(this.openapi, 'info.title', COLLECTION_NAME),
205+
name: name,
200206
output: [{
201207
type: 'collection',
202-
name: _.get(this.openapi, 'info.title', COLLECTION_NAME)
208+
name: name
203209
}]
204210
});
205211
}
@@ -213,12 +219,18 @@ class SchemaPack {
213219
return cb(null, validationResult);
214220
}
215221

222+
let name = _.get(this.openapi, 'info.title');
223+
224+
if (_.isEmpty(name) || !_.isString(name)) {
225+
name = COLLECTION_NAME;
226+
}
227+
216228
return cb(null, {
217229
result: true,
218-
name: _.get(this.openapi, 'info.title', COLLECTION_NAME),
230+
name: name,
219231
output: [{
220232
type: 'collection',
221-
name: _.get(this.openapi, 'info.title', COLLECTION_NAME)
233+
name: name
222234
}]
223235
});
224236
});
@@ -336,11 +348,17 @@ class SchemaPack {
336348
// Fix {scheme} and {path} vars in the URL to :scheme and :path
337349
openapi.baseUrl = schemaUtils.fixPathVariablesInUrl(openapi.baseUrl);
338350

351+
let name = _.get(this.openapi, 'info.title');
352+
353+
if (_.isEmpty(name) || !_.isString(name)) {
354+
name = COLLECTION_NAME;
355+
}
356+
339357
// Creating a new instance of a Postman collection
340358
// All generated folders and requests will go inside this
341359
generatedStore.collection = new sdk.Collection({
342360
info: {
343-
name: _.isEmpty(_.get(openapi, 'info.title')) ? COLLECTION_NAME : _.get(openapi, 'info.title')
361+
name: name
344362
}
345363
});
346364

libV2/helpers/collection/generateCollectionFromOpenAPI.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const _ = require('lodash'),
22
generateAuthrForCollectionFromOpenAPI = require('./generateAuthForCollectionFromOpenAPI'),
3-
COLLECTION_NAME = 'Imported from OpenAPI 3.0',
3+
COLLECTION_NAME = 'Imported from OpenAPI',
44

55
/**
66
* Returns a description that's usable at the collection-level
@@ -93,10 +93,16 @@ module.exports = function ({ openapi }) {
9393

9494
const collectionVariables = resolveCollectionVariablesForBaseUrlFromServersObject(_.get(openapi, 'servers.0'));
9595

96+
let name = _.get(this.openapi, 'info.title');
97+
98+
if (_.isEmpty(name) || !_.isString(name)) {
99+
name = COLLECTION_NAME;
100+
}
101+
96102
return {
97103
data: {
98104
info: {
99-
name: _.get(openapi, 'info.title', COLLECTION_NAME),
105+
name: name,
100106
description: getCollectionDescription(openapi)
101107
},
102108
auth: generateAuthrForCollectionFromOpenAPI(openapi, openapi.security)
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: ''
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
format: int32
24+
- name: variable
25+
in: query
26+
description: random variable
27+
style: form
28+
explode: false
29+
schema:
30+
type: array
31+
items:
32+
type: string
33+
- name: variable2
34+
in: query
35+
description: another random variable
36+
style: spaceDelimited
37+
schema:
38+
type: array
39+
items:
40+
type: integer
41+
format: int64
42+
responses:
43+
'200':
44+
description: An paged array of pets
45+
headers:
46+
x-next:
47+
description: A link to the next page of responses
48+
schema:
49+
type: string
50+
content:
51+
application/json:
52+
schema:
53+
$ref: "https://postman-echo.com/get"
54+
default:
55+
description: unexpected error
56+
content:
57+
application/json:
58+
schema:
59+
$ref: "#/components/schemas/Error"
60+
post:
61+
summary: Create a pet
62+
operationId: createPets
63+
tags:
64+
- pets
65+
responses:
66+
'201':
67+
description: Null response
68+
default:
69+
description: unexpected error
70+
content:
71+
application/json:
72+
schema:
73+
$ref: "#/components/schemas/Error"
74+
/pets/{petId}:
75+
get:
76+
summary: Info for a specific pet
77+
operationId: showPetById
78+
tags:
79+
- pets
80+
parameters:
81+
- name: petId
82+
in: path
83+
required: true
84+
description: The id of the pet to retrieve
85+
schema:
86+
type: string
87+
responses:
88+
'200':
89+
description: Expected response to a valid request
90+
content:
91+
application/json:
92+
schema:
93+
$ref: "#/components/schemas/Pets"
94+
default:
95+
description: unexpected error
96+
content:
97+
application/json:
98+
schema:
99+
$ref: "#/components/schemas/Error"
100+
components:
101+
schemas:
102+
Pet:
103+
required:
104+
- id
105+
- name
106+
properties:
107+
id:
108+
type: integer
109+
format: int64
110+
name:
111+
type: string
112+
tag:
113+
type: string
114+
Pets:
115+
type: array
116+
items:
117+
$ref: "#/components/schemas/Pet"
118+
Error:
119+
required:
120+
- code
121+
- message
122+
properties:
123+
code:
124+
type: integer
125+
format: int32
126+
message:
127+
type: string

test/unit/base.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,27 @@ describe('CONVERT FUNCTION TESTS ', function() {
758758
});
759759
});
760760

761+
it('Should return validation result for an definition with empty title field', function(done) {
762+
var invalidNoInfo = path.join(__dirname, VALID_OPENAPI_PATH + '/empty-title.yaml'),
763+
openapi = fs.readFileSync(invalidNoInfo, 'utf8');
764+
Converter.getMetaData({ type: 'json', data: openapi }, (err, status) => {
765+
if (err) {
766+
expect.fail(null, null, err);
767+
}
768+
if (status.result) {
769+
expect(status.result).to.be.eq(true);
770+
expect(status.name).to.be.equal('Imported from OpenAPI');
771+
expect(status.output[0].name).to.be.equal('Imported from OpenAPI');
772+
expect(status.output[0].type).to.be.equal('collection');
773+
done();
774+
}
775+
else {
776+
expect.fail(null, null, status.reason);
777+
done();
778+
}
779+
});
780+
});
781+
761782
it('Should return error for more than one root files', function(done) {
762783
let folderPath = path.join(__dirname, '../data/multiFile_with_two_root'),
763784
array = [
@@ -2147,6 +2168,25 @@ describe('CONVERT FUNCTION TESTS ', function() {
21472168
done();
21482169
});
21492170
});
2171+
2172+
it('Should convert definition with empty title field correctly with default name', function(done) {
2173+
const fileData = fs.readFileSync(path.join(__dirname, VALID_OPENAPI_PATH, 'empty-title.yaml'), 'utf8'),
2174+
input = {
2175+
type: 'string',
2176+
data: fileData
2177+
};
2178+
2179+
Converter.convert(input, { }, (error, result) => {
2180+
expect(error).to.be.null;
2181+
expect(result.result).to.equal(true);
2182+
expect(result.output.length).to.equal(1);
2183+
expect(result.output[0].type).to.have.equal('collection');
2184+
expect(result.output[0].data).to.have.property('info');
2185+
expect(result.output[0].data.info.name).to.eql('Imported from OpenAPI');
2186+
expect(result.output[0].data).to.have.property('item');
2187+
done();
2188+
});
2189+
});
21502190
});
21512191

21522192
describe('requestNameSource option', function() {

test/unit/convertV2.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,4 +2310,23 @@ describe('The convert v2 Function', function() {
23102310
done();
23112311
});
23122312
});
2313+
2314+
it('Should convert definition with empty title field correctly with default name', function(done) {
2315+
const fileData = fs.readFileSync(path.join(__dirname, VALID_OPENAPI_PATH, 'empty-title.yaml'), 'utf8'),
2316+
input = {
2317+
type: 'string',
2318+
data: fileData
2319+
};
2320+
2321+
Converter.convert(input, { }, (error, result) => {
2322+
expect(error).to.be.null;
2323+
expect(result.result).to.equal(true);
2324+
expect(result.output.length).to.equal(1);
2325+
expect(result.output[0].type).to.have.equal('collection');
2326+
expect(result.output[0].data).to.have.property('info');
2327+
expect(result.output[0].data.info.name).to.eql('Imported from OpenAPI');
2328+
expect(result.output[0].data).to.have.property('item');
2329+
done();
2330+
});
2331+
});
23132332
});

0 commit comments

Comments
 (0)