Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 44e5ca0

Browse files
authored
Merge pull request #273 from apiaryio/kylef/pattern
Work around infinite loop bug when using pattern and minLength in OAS 2
2 parents 8423874 + fffe8f2 commit 44e5ca0

File tree

6 files changed

+264
-2
lines changed

6 files changed

+264
-2
lines changed

packages/fury-adapter-swagger/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Fury Swagger Parser Changelog
22

3+
## 0.27.1 (2019-06-03)
4+
5+
### Bug Fixes
6+
7+
- Fixes a problem while parsing a document which contains a Swagger Schema for
8+
a string which contains both a `minLength` and a `pattern` property which are
9+
incompatible. For example, the following pattern: `^[A-z]*$` which is making
10+
use of `*` which means that it allows strings that are zero length or more.
11+
If there is a property `minLength` which is incompatible with the pattern
12+
such as if `minLength` is set to 1. Previously this would cause the parser to
13+
get into an infinite loop.
14+
315
## 0.26.0 (2019-06-11)
416

517
### Breaking

packages/fury-adapter-swagger/lib/json-schema.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ const convertSubSchema = (schema, references, swagger) => {
166166
actualSchema.type = 'string';
167167
}
168168

169+
if (schema.pattern && schema.minLength && schema.pattern.startsWith('^[') && schema.pattern.endsWith(']*$')) {
170+
// If a schema has a minimal length (minLength) > 0 AND there is a regex
171+
// such as: `^[A-z]*$`, the schema can resolve to an empty string which
172+
// doesn't match minLength.
173+
//
174+
// JSON Schema Faker will fail in that case and get into an infinite loop.
175+
actualSchema.pattern = schema.pattern.replace('*$', '+$');
176+
}
177+
169178
if (schema.example) {
170179
actualSchema.examples = [dereference(schema.example, swagger)];
171180
}

packages/fury-adapter-swagger/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fury-adapter-swagger",
3-
"version": "0.27.0",
3+
"version": "0.27.1",
44
"description": "Swagger 2.0 parser for Fury.js",
55
"author": "Apiary.io <[email protected]>",
66
"license": "MIT",
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
{
2+
"element": "parseResult",
3+
"content": [
4+
{
5+
"element": "category",
6+
"meta": {
7+
"classes": {
8+
"element": "array",
9+
"content": [
10+
{
11+
"element": "string",
12+
"content": "api"
13+
}
14+
]
15+
},
16+
"title": {
17+
"element": "string",
18+
"content": "Produces JSON with pattern"
19+
}
20+
},
21+
"attributes": {
22+
"version": {
23+
"element": "string",
24+
"content": "1.0"
25+
}
26+
},
27+
"content": [
28+
{
29+
"element": "resource",
30+
"attributes": {
31+
"href": {
32+
"element": "string",
33+
"content": "/test"
34+
}
35+
},
36+
"content": [
37+
{
38+
"element": "transition",
39+
"content": [
40+
{
41+
"element": "httpTransaction",
42+
"content": [
43+
{
44+
"element": "httpRequest",
45+
"attributes": {
46+
"method": {
47+
"element": "string",
48+
"content": "GET"
49+
},
50+
"headers": {
51+
"element": "httpHeaders",
52+
"content": [
53+
{
54+
"element": "member",
55+
"meta": {
56+
"links": {
57+
"element": "array",
58+
"content": [
59+
{
60+
"element": "link",
61+
"attributes": {
62+
"relation": {
63+
"element": "string",
64+
"content": "inferred"
65+
},
66+
"href": {
67+
"element": "string",
68+
"content": "http://docs.apiary.io/validations/swagger#produces-accept"
69+
}
70+
}
71+
}
72+
]
73+
}
74+
},
75+
"content": {
76+
"key": {
77+
"element": "string",
78+
"content": "Accept"
79+
},
80+
"value": {
81+
"element": "string",
82+
"content": "application/json"
83+
}
84+
}
85+
}
86+
]
87+
}
88+
}
89+
},
90+
{
91+
"element": "httpResponse",
92+
"attributes": {
93+
"headers": {
94+
"element": "httpHeaders",
95+
"content": [
96+
{
97+
"element": "member",
98+
"meta": {
99+
"links": {
100+
"element": "array",
101+
"content": [
102+
{
103+
"element": "link",
104+
"attributes": {
105+
"relation": {
106+
"element": "string",
107+
"content": "inferred"
108+
},
109+
"href": {
110+
"element": "string",
111+
"content": "http://docs.apiary.io/validations/swagger#produces-content-type"
112+
}
113+
}
114+
}
115+
]
116+
}
117+
},
118+
"content": {
119+
"key": {
120+
"element": "string",
121+
"content": "Content-Type"
122+
},
123+
"value": {
124+
"element": "string",
125+
"content": "application/json"
126+
}
127+
}
128+
}
129+
]
130+
},
131+
"statusCode": {
132+
"element": "string",
133+
"content": "200"
134+
}
135+
},
136+
"content": [
137+
{
138+
"element": "copy",
139+
"content": "My Response"
140+
},
141+
{
142+
"element": "asset",
143+
"meta": {
144+
"classes": {
145+
"element": "array",
146+
"content": [
147+
{
148+
"element": "string",
149+
"content": "messageBody"
150+
}
151+
]
152+
},
153+
"links": {
154+
"element": "array",
155+
"content": [
156+
{
157+
"element": "link",
158+
"attributes": {
159+
"relation": {
160+
"element": "string",
161+
"content": "inferred"
162+
},
163+
"href": {
164+
"element": "string",
165+
"content": "http://docs.apiary.io/validations/swagger#message-body-generation"
166+
}
167+
}
168+
}
169+
]
170+
}
171+
},
172+
"attributes": {
173+
"contentType": {
174+
"element": "string",
175+
"content": "application/json"
176+
}
177+
},
178+
"content": "A"
179+
},
180+
{
181+
"element": "asset",
182+
"meta": {
183+
"classes": {
184+
"element": "array",
185+
"content": [
186+
{
187+
"element": "string",
188+
"content": "messageBodySchema"
189+
}
190+
]
191+
}
192+
},
193+
"attributes": {
194+
"contentType": {
195+
"element": "string",
196+
"content": "application/schema+json"
197+
}
198+
},
199+
"content": "{\"type\":\"string\",\"minLength\":1,\"maxLength\":255,\"pattern\":\"^[A-z]+$\"}"
200+
},
201+
{
202+
"element": "dataStructure",
203+
"content": {
204+
"element": "string",
205+
"meta": {
206+
"description": {
207+
"element": "string",
208+
"content": "- Matches regex pattern: `^[A-z]*$`\n- Length of string must be less than, or equal to 255\n- Length of string must be greater than, or equal to 1"
209+
}
210+
}
211+
}
212+
}
213+
]
214+
}
215+
]
216+
}
217+
]
218+
}
219+
]
220+
}
221+
]
222+
}
223+
]
224+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
swagger: '2.0'
2+
info:
3+
title: Produces JSON with pattern
4+
version: '1.0'
5+
produces:
6+
- application/json
7+
paths:
8+
'/test':
9+
get:
10+
responses:
11+
200:
12+
description: 'My Response'
13+
schema:
14+
type: string
15+
minLength: 1
16+
maxLength: 255
17+
pattern: ^[A-z]*$

packages/fury-cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"fury-adapter-apib-parser": "^0.16.0",
2727
"fury-adapter-apib-serializer": "^0.12.0",
2828
"fury-adapter-oas3-parser": "^0.9.0",
29-
"fury-adapter-swagger": "^0.27.0",
29+
"fury-adapter-swagger": "^0.27.1",
3030
"js-yaml": "^3.12.0",
3131
"minim": "^0.23.4"
3232
},

0 commit comments

Comments
 (0)