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

Commit 3a1727d

Browse files
committed
fix(oas3): handle negative boolean values
1 parent f1d81c3 commit 3a1727d

File tree

5 files changed

+86
-24
lines changed

5 files changed

+86
-24
lines changed

packages/openapi3-parser/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
invalid: `style`, `explode`, `allowReserved`, `schema`, `content`, `example`,
99
and `examples`. These will now return an unsupported warning instead.
1010

11+
- Negative boolean YAML values was previously treated as true and thus
12+
Parameter Object's with `required: false` would have been incorrectly treated
13+
as required.
14+
1115
## 0.13.0 (2020-06-12)
1216

1317
The package has been updated for compatibility with `@apielements/core`.

packages/openapi3-parser/lib/parser/parseYAML.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function convert(node, annotations, context) {
6161
} else if (node.tag === 'tag:yaml.org,2002:int' || node.tag === 'tag:yaml.org,2002:float') {
6262
element = new namespace.elements.Number(Number(node.value));
6363
} else if (node.tag === 'tag:yaml.org,2002:bool') {
64-
element = new namespace.elements.Boolean(Boolean(node.value));
64+
element = new namespace.elements.Boolean(node.value === 'true' || node.value === 'yes' || node.value === 'on');
6565
} else if (node.tag === 'tag:yaml.org,2002:null') {
6666
element = new namespace.elements.Null();
6767
} else if (node.tag === 'tag:yaml.org,2002:binary') {

packages/openapi3-parser/test/integration/fixtures/petstore.json

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,6 @@
122122
"content": "How many items to return at one time (max 100)"
123123
}
124124
},
125-
"attributes": {
126-
"typeAttributes": {
127-
"element": "array",
128-
"content": [
129-
{
130-
"element": "string",
131-
"content": "required"
132-
}
133-
]
134-
}
135-
},
136125
"content": {
137126
"key": {
138127
"element": "string",

packages/openapi3-parser/test/integration/fixtures/petstore.sourcemap.json

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -347,17 +347,6 @@
347347
"content": "How many items to return at one time (max 100)"
348348
}
349349
},
350-
"attributes": {
351-
"typeAttributes": {
352-
"element": "array",
353-
"content": [
354-
{
355-
"element": "string",
356-
"content": "required"
357-
}
358-
]
359-
}
360-
},
361350
"content": {
362351
"key": {
363352
"element": "string",

packages/openapi3-parser/test/unit/parser/parseYAML-test.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('#parseYAML', () => {
9393
expect(element.first).to.have.sourceMapEndColumn(4);
9494
});
9595

96-
it('can parse an boolean value into a boolean element', () => {
96+
it('can parse an boolean yes value into a boolean element', () => {
9797
const element = parseYAML('yes', context);
9898

9999
expect(element).to.be.instanceof(namespace.elements.ParseResult);
@@ -109,6 +109,86 @@ describe('#parseYAML', () => {
109109
expect(element.first).to.have.sourceMapEndColumn(4);
110110
});
111111

112+
it('can parse an boolean on value into a boolean element', () => {
113+
const element = parseYAML('on', context);
114+
115+
expect(element).to.be.instanceof(namespace.elements.ParseResult);
116+
expect(element.length).to.equal(1);
117+
118+
expect(element.first).to.be.instanceof(namespace.elements.Boolean);
119+
expect(element.first.toValue()).to.deep.equal(true);
120+
expect(element.first).to.have.sourceMapStart(0);
121+
expect(element.first).to.have.sourceMapOffset(2);
122+
expect(element.first).to.have.sourceMapStartLine(1);
123+
expect(element.first).to.have.sourceMapStartColumn(1);
124+
expect(element.first).to.have.sourceMapEndLine(1);
125+
expect(element.first).to.have.sourceMapEndColumn(3);
126+
});
127+
128+
it('can parse an boolean true value into a boolean element', () => {
129+
const element = parseYAML('true', context);
130+
131+
expect(element).to.be.instanceof(namespace.elements.ParseResult);
132+
expect(element.length).to.equal(1);
133+
134+
expect(element.first).to.be.instanceof(namespace.elements.Boolean);
135+
expect(element.first.toValue()).to.deep.equal(true);
136+
expect(element.first).to.have.sourceMapStart(0);
137+
expect(element.first).to.have.sourceMapOffset(4);
138+
expect(element.first).to.have.sourceMapStartLine(1);
139+
expect(element.first).to.have.sourceMapStartColumn(1);
140+
expect(element.first).to.have.sourceMapEndLine(1);
141+
expect(element.first).to.have.sourceMapEndColumn(5);
142+
});
143+
144+
it('can parse an boolean false value into a boolean element', () => {
145+
const element = parseYAML('false', context);
146+
147+
expect(element).to.be.instanceof(namespace.elements.ParseResult);
148+
expect(element.length).to.equal(1);
149+
150+
expect(element.first).to.be.instanceof(namespace.elements.Boolean);
151+
expect(element.first.toValue()).to.deep.equal(false);
152+
expect(element.first).to.have.sourceMapStart(0);
153+
expect(element.first).to.have.sourceMapOffset(5);
154+
expect(element.first).to.have.sourceMapStartLine(1);
155+
expect(element.first).to.have.sourceMapStartColumn(1);
156+
expect(element.first).to.have.sourceMapEndLine(1);
157+
expect(element.first).to.have.sourceMapEndColumn(6);
158+
});
159+
160+
it('can parse an boolean no value into a boolean element', () => {
161+
const element = parseYAML('no', context);
162+
163+
expect(element).to.be.instanceof(namespace.elements.ParseResult);
164+
expect(element.length).to.equal(1);
165+
166+
expect(element.first).to.be.instanceof(namespace.elements.Boolean);
167+
expect(element.first.toValue()).to.deep.equal(false);
168+
expect(element.first).to.have.sourceMapStart(0);
169+
expect(element.first).to.have.sourceMapOffset(2);
170+
expect(element.first).to.have.sourceMapStartLine(1);
171+
expect(element.first).to.have.sourceMapStartColumn(1);
172+
expect(element.first).to.have.sourceMapEndLine(1);
173+
expect(element.first).to.have.sourceMapEndColumn(3);
174+
});
175+
176+
it('can parse an boolean off value into a boolean element', () => {
177+
const element = parseYAML('off', context);
178+
179+
expect(element).to.be.instanceof(namespace.elements.ParseResult);
180+
expect(element.length).to.equal(1);
181+
182+
expect(element.first).to.be.instanceof(namespace.elements.Boolean);
183+
expect(element.first.toValue()).to.deep.equal(false);
184+
expect(element.first).to.have.sourceMapStart(0);
185+
expect(element.first).to.have.sourceMapOffset(3);
186+
expect(element.first).to.have.sourceMapStartLine(1);
187+
expect(element.first).to.have.sourceMapStartColumn(1);
188+
expect(element.first).to.have.sourceMapEndLine(1);
189+
expect(element.first).to.have.sourceMapEndColumn(4);
190+
});
191+
112192
it('can parse null into a null element', () => {
113193
const element = parseYAML('null', context);
114194

0 commit comments

Comments
 (0)