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

Commit

Permalink
fix(oas3): handle missing variables in Server Object url
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Apr 30, 2020
1 parent 00190dd commit 76e1d26
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
17 changes: 17 additions & 0 deletions packages/openapi3-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Fury OAS3 Parser Changelog

## 0.12.1 (2020-04-30)

### Bug Fixes

- Prevent the parser from throwing an error when handling a Server Object with
variables when the URL does not contain any variables. For example:

```yaml
openapi: 3.0.3
servers:
- url: https://example.com
variables:
version:
default: '1.0'
paths: {}
```
## 0.12.0 (2020-04-29)
The package has been renamed to `@apielements/openapi3-parser`.
Expand Down
14 changes: 11 additions & 3 deletions packages/openapi3-parser/lib/parser/oas/parseServerObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ const parseServerVariableObject = require('./parseServerVariableObject');
const name = 'Server Object';
const requiredKeys = ['url'];

function extractURLVariables(path) {
const matches = path.match(/({.*?})/gm);

if (matches) {
return matches.map(x => x.substring(1, x.length - 1));
}

return [];
}

const validateVariablesInURL = (context, object) => {
const url = object.getValue('url');
const variables = object.get('variables');
const parseResult = new context.namespace.elements.ParseResult();

const urlVariables = url
.match(/{(.*?)}/g)
.map(x => x.replace(/[{}]/g, ''));
const urlVariables = extractURLVariables(url);

// if you define a variable that is not in URL it warns (and the variable is ignored).
variables.keys().forEach((key) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi3-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apielements/openapi3-parser",
"version": "0.12.0",
"version": "0.12.1",
"description": "Open API Specification 3 API Elements Parser",
"author": "Apiary.io <[email protected]>",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ describe('#parseServerObject', () => {
expect(secondHrefVariable.value.default).to.equal('1.0');
});

it('warns when variables is defined, but no variables are in the url', () => {
const server = new namespace.elements.Object({
url: 'https://example.com/',
variables: {
version: {
default: '1.0',
},
},
});

const parseResult = parse(context)(server);
expect(parseResult).to.contain.warning("Server variable 'version' is not present in the URL and will be ignored");

const resource = parseResult.get(0);
expect(resource).to.be.instanceof(namespace.elements.Resource);

expect(resource.href.toValue()).to.equal('https://example.com/');
expect(resource.hrefVariables.isEmpty).to.be.true;
});

it("warns when a URL defined variable is missing from 'variables'", () => {
const server = new namespace.elements.Object({
url: 'https://{username}.{server}/{version}/',
Expand Down

0 comments on commit 76e1d26

Please sign in to comment.