Skip to content

Commit 14f0eb0

Browse files
authored
Merge pull request #491 from postmanlabs/fix10672/patternKeyInSchema
Fix #10672, Adding support to schemas with pattern key as schema prop…
2 parents 820027a + d9b4de9 commit 14f0eb0

File tree

3 files changed

+240
-0
lines changed

3 files changed

+240
-0
lines changed

assets/json-schema-faker.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23469,6 +23469,9 @@ function extend() {
2346923469
var fn = keys[length].replace(/^x-/, '');
2347023470
var gen = this.support[fn];
2347123471
if (typeof gen === 'function') {
23472+
if (typeof schema[fn] === 'object' && schema[fn].hasOwnProperty('type')) {
23473+
continue;
23474+
}
2347223475
Object.defineProperty(schema, 'generate', {
2347323476
configurable: false,
2347423477
enumerable: false,
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Swagger Petstore issue 10672",
6+
"license": {
7+
"name": "MIT"
8+
}
9+
},
10+
"servers": [
11+
{
12+
"url": "http://petstore.swagger.io/v1"
13+
}
14+
],
15+
"paths": {
16+
"/pets": {
17+
"get": {
18+
"summary": "List all pets",
19+
"operationId": "listPets",
20+
"tags": [
21+
"pets"
22+
],
23+
"parameters": [
24+
{
25+
"name": "limit",
26+
"in": "header",
27+
"description": "How many items to return at one time (max 100)",
28+
"required": false,
29+
"schema": {
30+
"type": "integer",
31+
"format": "int32"
32+
}
33+
},
34+
{
35+
"name": "variable",
36+
"in": "query",
37+
"description": "random variable",
38+
"style": "form",
39+
"explode": false,
40+
"schema": {
41+
"type": "array",
42+
"items": {
43+
"type": "string"
44+
}
45+
}
46+
},
47+
{
48+
"name": "variable2",
49+
"in": "query",
50+
"description": "another random variable",
51+
"style": "spaceDelimited",
52+
"schema": {
53+
"type": "array",
54+
"items": {
55+
"type": "integer",
56+
"format": "int64"
57+
}
58+
}
59+
}
60+
],
61+
"responses": {
62+
"200": {
63+
"description": "An paged array of pets",
64+
"headers": {
65+
"x-next": {
66+
"description": "A link to the next page of responses",
67+
"schema": {
68+
"type": "string"
69+
}
70+
}
71+
},
72+
"content": {
73+
"application/json": {
74+
"schema": {
75+
"$ref": "#/components/schemas/Pets"
76+
}
77+
}
78+
}
79+
},
80+
"default": {
81+
"description": "unexpected error",
82+
"content": {
83+
"application/json": {
84+
"schema": {
85+
"$ref": "#/components/schemas/Error"
86+
}
87+
}
88+
}
89+
}
90+
}
91+
},
92+
"post": {
93+
"summary": "Create a pet",
94+
"operationId": "createPets",
95+
"tags": [
96+
"pets"
97+
],
98+
"responses": {
99+
"201": {
100+
"description": "Null response"
101+
},
102+
"default": {
103+
"description": "unexpected error",
104+
"content": {
105+
"application/json": {
106+
"schema": {
107+
"$ref": "#/components/schemas/Error"
108+
}
109+
}
110+
}
111+
}
112+
}
113+
},
114+
"parameters": [
115+
{
116+
"name": "limit_2",
117+
"in": "headers",
118+
"description": "How many items to return at one time (max 100)",
119+
"required": false,
120+
"schema": {
121+
"type": "integer",
122+
"format": "int32"
123+
}
124+
}
125+
]
126+
},
127+
"/pets/{petId}": {
128+
"get": {
129+
"summary": "Info for a specific pet",
130+
"operationId": "showPetById",
131+
"tags": [
132+
"pets"
133+
],
134+
"parameters": [
135+
{
136+
"name": "petId",
137+
"in": "path",
138+
"required": true,
139+
"description": "The id of the pet to retrieve",
140+
"schema": {
141+
"type": "string"
142+
}
143+
}
144+
],
145+
"responses": {
146+
"200": {
147+
"description": "Expected response to a valid request",
148+
"content": {
149+
"application/json": {
150+
"schema": {
151+
"$ref": "#/components/schemas/Pet"
152+
}
153+
}
154+
}
155+
},
156+
"default": {
157+
"description": "unexpected error",
158+
"content": {
159+
"application/json": {
160+
"schema": {
161+
"$ref": "#/components/schemas/Error"
162+
}
163+
}
164+
}
165+
}
166+
}
167+
}
168+
}
169+
},
170+
"components": {
171+
"schemas": {
172+
"Pet": {
173+
"required": [
174+
"id",
175+
"name"
176+
],
177+
"properties": {
178+
"id": {
179+
"type": "integer",
180+
"format": "int64"
181+
},
182+
"name": {
183+
"type": "string"
184+
},
185+
"tag": {
186+
"type": "string"
187+
},
188+
"pattern": {
189+
"type": "string"
190+
}
191+
}
192+
},
193+
"Pets": {
194+
"type": "array",
195+
"items": {
196+
"$ref": "#/components/schemas/Pet"
197+
}
198+
},
199+
"Error": {
200+
"required": [
201+
"code",
202+
"message"
203+
],
204+
"properties": {
205+
"code": {
206+
"type": "integer",
207+
"format": "int32"
208+
},
209+
"message": {
210+
"type": "string"
211+
}
212+
}
213+
}
214+
}
215+
}
216+
}

test/unit/base.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('CONVERT FUNCTION TESTS ', function() {
1616
test31SpecDir = path.join(__dirname, '../data/valid_openapi31X/'),
1717
issue133 = path.join(__dirname, VALID_OPENAPI_PATH + '/issue#133.json'),
1818
issue160 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#160.json'),
19+
issue10672 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#10672.json'),
1920
unique_items_schema = path.join(__dirname, VALID_OPENAPI_PATH + '/unique_items_schema.json'),
2021
serverOverRidingSpec = path.join(__dirname, VALID_OPENAPI_PATH + '/server_overriding.json'),
2122
infoHavingContactOnlySpec = path.join(__dirname, VALID_OPENAPI_PATH + '/info_having_contact_only.json'),
@@ -872,6 +873,26 @@ describe('CONVERT FUNCTION TESTS ', function() {
872873
});
873874
});
874875

876+
it('[GITHUB #10672] Should convert a collection with a key "pattern" in a schema', function() {
877+
const fileSource = issue10672,
878+
fileData = fs.readFileSync(fileSource, 'utf8'),
879+
input = {
880+
type: 'string',
881+
data: fileData
882+
};
883+
884+
Converter.convert(input, {}, (err, result) => {
885+
expect(err).to.be.null;
886+
let body = JSON.parse(result.output[0].data.item[0].item[0].response[0].body);
887+
expect(result.result).to.be.true;
888+
expect(body)
889+
.to.be.an('array').with.length(2);
890+
expect(body.filter((item) => {
891+
return item.pattern && typeof item.pattern === 'string';
892+
})).to.be.an('array').with.length(2);
893+
});
894+
});
895+
875896
it('should not return undefined in the error message if spec is not valid JSON/YAML', function(done) {
876897
// invalid JSON
877898
Converter.convert({ type: 'string', data: '{"key": { "value" : } ' }, {}, (err, conversionResult) => {

0 commit comments

Comments
 (0)