Skip to content

Commit 298f731

Browse files
authored
Merge pull request #487 from postmanlabs/fix10229/falsyExamples
Fix10229/falsy examples
2 parents 155d194 + e39b964 commit 298f731

File tree

3 files changed

+216
-2
lines changed

3 files changed

+216
-2
lines changed

lib/schemaUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ module.exports = {
406406
let example = _.get(parameter, 'example'),
407407
examples = _.values(_.get(parameter, 'examples'));
408408

409-
if (example) {
409+
if (example !== undefined) {
410410
_.set(parameter, 'schema.example', example);
411411
}
412412
else if (examples) {
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "#10229",
6+
"license": {
7+
"name": "MIT"
8+
}
9+
},
10+
"servers": [
11+
{
12+
"url": "http://petstore.swagger.io/v1"
13+
}
14+
],
15+
"paths": {
16+
"/pets": {
17+
"servers": [
18+
{
19+
"url": "http://petstore.swagger.io:{port}/{basePath}",
20+
"variables": {
21+
"port": {
22+
"enum": [
23+
"8443",
24+
"443"
25+
],
26+
"default": "8443"
27+
},
28+
"basePath": {
29+
"default": "v2"
30+
}
31+
}
32+
}
33+
],
34+
"get": {
35+
"summary": "List all pets",
36+
"operationId": "listPets",
37+
"tags": [
38+
"pets"
39+
],
40+
"parameters": [
41+
{
42+
"name": "limit",
43+
"in": "query",
44+
"description": "How many items to return at one time (max 100)",
45+
"required": false,
46+
"schema": {
47+
"type": "integer",
48+
"format": "int32"
49+
},
50+
"example": 0
51+
},
52+
{
53+
"name": "otherString",
54+
"in": "query",
55+
"description": "other parameter",
56+
"required": false,
57+
"schema": {
58+
"type": "string"
59+
},
60+
"example": ""
61+
},
62+
{
63+
"name": "otherBoolean",
64+
"in": "query",
65+
"description": "other parameter",
66+
"required": false,
67+
"schema": {
68+
"type": "boolean"
69+
},
70+
"example": false
71+
}
72+
],
73+
"responses": {
74+
"200": {
75+
"description": "A paged array of pets",
76+
"headers": {
77+
"x-next": {
78+
"description": "A link to the next page of responses",
79+
"schema": {
80+
"type": "string"
81+
}
82+
}
83+
},
84+
"content": {
85+
"application/json": {
86+
"schema": {
87+
"$ref": "#/components/schemas/Pets"
88+
}
89+
}
90+
}
91+
},
92+
"default": {
93+
"description": "unexpected error",
94+
"content": {
95+
"application/json": {
96+
"schema": {
97+
"$ref": "#/components/schemas/Error"
98+
}
99+
}
100+
}
101+
}
102+
}
103+
},
104+
"post": {
105+
"summary": "Create a pet",
106+
"operationId": "createPets",
107+
"tags": [
108+
"pets"
109+
],
110+
"requestBody": {
111+
"content": {
112+
"application/json": {
113+
"schema": {
114+
"type": "object",
115+
"properties": {
116+
"a": {
117+
"type": "null",
118+
"example": null
119+
}
120+
}
121+
}
122+
}
123+
}
124+
},
125+
"responses": {
126+
"201": {
127+
"description": "Null response"
128+
},
129+
"default": {
130+
"description": "unexpected error",
131+
"content": {
132+
"application/json": {
133+
"schema": {
134+
"type": "object",
135+
"properties": {
136+
"a": {
137+
"type": "null",
138+
"example": null
139+
}
140+
}
141+
}
142+
}
143+
}
144+
}
145+
}
146+
}
147+
}
148+
},
149+
"components": {
150+
"schemas": {
151+
"Pet": {
152+
"type": "object",
153+
"required": [
154+
"id",
155+
"name"
156+
],
157+
"properties": {
158+
"id": {
159+
"type": "integer",
160+
"format": "int64"
161+
},
162+
"name": {
163+
"type": "string"
164+
},
165+
"tag": {
166+
"type": "string"
167+
}
168+
}
169+
},
170+
"Pets": {
171+
"type": "array",
172+
"items": {
173+
"$ref": "#/components/schemas/Pet"
174+
}
175+
},
176+
"Error": {
177+
"type": "object",
178+
"required": [
179+
"code",
180+
"message"
181+
],
182+
"properties": {
183+
"code": {
184+
"type": "integer",
185+
"format": "int32"
186+
},
187+
"message": {
188+
"type": "string"
189+
}
190+
}
191+
}
192+
}
193+
}
194+
}

test/unit/base.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ describe('CONVERT FUNCTION TESTS ', function() {
4343
securityTestCases = path.join(__dirname, VALID_OPENAPI_PATH + '/security-test-cases.yaml'),
4444
emptySecurityTestCase = path.join(__dirname, VALID_OPENAPI_PATH + '/empty-security-test-case.yaml'),
4545
rootUrlServerWithVariables = path.join(__dirname, VALID_OPENAPI_PATH + '/root_url_server_with_variables.json'),
46-
parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml');
46+
parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml'),
47+
issue10229 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#10229.json');
4748

4849

4950
it('Should add collection level auth with type as `bearer`' +
@@ -393,6 +394,25 @@ describe('CONVERT FUNCTION TESTS ', function() {
393394
done();
394395
});
395396
});
397+
it('#GITHUB-10229 should generate correct example is out of the schema and is falsy' +
398+
issue10229, function(done) {
399+
var openapi = fs.readFileSync(issue10229, 'utf8');
400+
Converter.convert({ type: 'string', data: openapi }, { requestParametersResolution: 'Example' },
401+
(err, conversionResult) => {
402+
expect(err).to.be.null;
403+
expect(conversionResult.result).to.equal(true);
404+
expect(conversionResult.output.length).to.equal(1);
405+
expect(conversionResult.output[0].type).to.equal('collection');
406+
expect(conversionResult.output[0].data).to.have.property('info');
407+
expect(conversionResult.output[0].data).to.have.property('item');
408+
expect(conversionResult.output[0].data.item[0].item[0].request.url.query[0].value).to.equal('0');
409+
expect(conversionResult.output[0].data.item[0].item[0].request.url.query[1].value).to.equal('');
410+
expect(conversionResult.output[0].data.item[0].item[0].request.url.query[2].value).to.equal('false');
411+
expect(conversionResult.output[0].data.item[0].item[1].request.body.raw).to.equal('{\n "a": null\n}');
412+
expect(conversionResult.output[0].data.item[0].item[1].response[1].body).to.equal('{\n "a": null\n}');
413+
done();
414+
});
415+
});
396416
describe('[Github #108]- Parameters resolution option', function() {
397417
it('Should respect schema faking for root request and example for example request' +
398418
examplesInSchemaSpec, function(done) {

0 commit comments

Comments
 (0)