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

Commit 76e1d26

Browse files
committed
fix(oas3): handle missing variables in Server Object url
1 parent 00190dd commit 76e1d26

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

packages/openapi3-parser/CHANGELOG.md

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

3+
## 0.12.1 (2020-04-30)
4+
5+
### Bug Fixes
6+
7+
- Prevent the parser from throwing an error when handling a Server Object with
8+
variables when the URL does not contain any variables. For example:
9+
10+
```yaml
11+
openapi: 3.0.3
12+
servers:
13+
- url: https://example.com
14+
variables:
15+
version:
16+
default: '1.0'
17+
paths: {}
18+
```
19+
320
## 0.12.0 (2020-04-29)
421
522
The package has been renamed to `@apielements/openapi3-parser`.

packages/openapi3-parser/lib/parser/oas/parseServerObject.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ const parseServerVariableObject = require('./parseServerVariableObject');
1515
const name = 'Server Object';
1616
const requiredKeys = ['url'];
1717

18+
function extractURLVariables(path) {
19+
const matches = path.match(/({.*?})/gm);
20+
21+
if (matches) {
22+
return matches.map(x => x.substring(1, x.length - 1));
23+
}
24+
25+
return [];
26+
}
27+
1828
const validateVariablesInURL = (context, object) => {
1929
const url = object.getValue('url');
2030
const variables = object.get('variables');
2131
const parseResult = new context.namespace.elements.ParseResult();
2232

23-
const urlVariables = url
24-
.match(/{(.*?)}/g)
25-
.map(x => x.replace(/[{}]/g, ''));
33+
const urlVariables = extractURLVariables(url);
2634

2735
// if you define a variable that is not in URL it warns (and the variable is ignored).
2836
variables.keys().forEach((key) => {

packages/openapi3-parser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apielements/openapi3-parser",
3-
"version": "0.12.0",
3+
"version": "0.12.1",
44
"description": "Open API Specification 3 API Elements Parser",
55
"author": "Apiary.io <support@apiary.io>",
66
"license": "MIT",

packages/openapi3-parser/test/unit/parser/oas/parseServerObject-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ describe('#parseServerObject', () => {
144144
expect(secondHrefVariable.value.default).to.equal('1.0');
145145
});
146146

147+
it('warns when variables is defined, but no variables are in the url', () => {
148+
const server = new namespace.elements.Object({
149+
url: 'https://example.com/',
150+
variables: {
151+
version: {
152+
default: '1.0',
153+
},
154+
},
155+
});
156+
157+
const parseResult = parse(context)(server);
158+
expect(parseResult).to.contain.warning("Server variable 'version' is not present in the URL and will be ignored");
159+
160+
const resource = parseResult.get(0);
161+
expect(resource).to.be.instanceof(namespace.elements.Resource);
162+
163+
expect(resource.href.toValue()).to.equal('https://example.com/');
164+
expect(resource.hrefVariables.isEmpty).to.be.true;
165+
});
166+
147167
it("warns when a URL defined variable is missing from 'variables'", () => {
148168
const server = new namespace.elements.Object({
149169
url: 'https://{username}.{server}/{version}/',

0 commit comments

Comments
 (0)