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

Add support for schema example in OAS3 parameters #459

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/openapi3-parser/lib/parser/oas/parseParameterObject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const R = require('ramda');
const {
isExtension, hasKey, hasValue, getValue,
isObject, isExtension, hasKey, hasValue, getValue,
} = require('../../predicates');
const {
createError,
Expand All @@ -17,7 +17,7 @@ const name = 'Parameter Object';
const requiredKeys = ['name', 'in'];
const unsupportedKeys = [
'deprecated', 'allowEmptyValue', 'style', 'allowReserved',
'schema', 'examples', 'content',
'examples', 'content',
];
const isUnsupportedKey = R.anyPass(R.map(hasKey, unsupportedKeys));

Expand Down Expand Up @@ -102,6 +102,7 @@ function parseParameterObject(context, object) {
[hasKey('required'), parseBoolean(context, name, false)],
[hasKey('explode'), parseBoolean(context, name, false)],
[hasKey('example'), e => e.clone()],
[hasKey('schema'), e => e.clone()],

[isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)],

Expand Down Expand Up @@ -163,7 +164,13 @@ function parseParameterObject(context, object) {
R.when(hasLocation('header'), validateHeaderName),
R.when(hasExplodeWithoutQueryIn, attachWarning(createUnsupportedExplodeWarning)),
(parameter) => {
const example = parameter.get('example');
let example = parameter.get('example');
if (!example) {
const schema = parameter.get('schema');
if (schema && isObject(schema)) {
example = schema.get('example');
}
}
const member = new namespace.elements.Member(parameter.get('name'), example);

const description = parameter.get('description');
Expand Down
120 changes: 0 additions & 120 deletions packages/openapi3-parser/test/integration/fixtures/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -857,66 +857,6 @@
},
"content": "'Operation Object' contains unsupported key 'tags'"
},
{
"element": "annotation",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "warning"
}
]
}
},
"attributes": {
"sourceMap": {
"element": "array",
"content": [
{
"element": "sourceMap",
"content": [
{
"element": "array",
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 21
},
"column": {
"element": "number",
"content": 11
}
},
"content": 414
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 21
},
"column": {
"element": "number",
"content": 17
}
},
"content": 6
}
]
}
]
}
]
}
},
"content": "'Parameter Object' contains unsupported key 'schema'"
},
{
"element": "annotation",
"meta": {
Expand Down Expand Up @@ -1097,66 +1037,6 @@
},
"content": "'Operation Object' contains unsupported key 'tags'"
},
{
"element": "annotation",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "warning"
}
]
}
},
"attributes": {
"sourceMap": {
"element": "array",
"content": [
{
"element": "sourceMap",
"content": [
{
"element": "array",
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 62
},
"column": {
"element": "number",
"content": 9
}
},
"content": 1504
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 62
},
"column": {
"element": "number",
"content": 15
}
},
"content": 6
}
]
}
]
}
]
}
},
"content": "'Parameter Object' contains unsupported key 'schema'"
},
{
"element": "annotation",
"meta": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1807,66 +1807,6 @@
},
"content": "'Operation Object' contains unsupported key 'tags'"
},
{
"element": "annotation",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "warning"
}
]
}
},
"attributes": {
"sourceMap": {
"element": "array",
"content": [
{
"element": "sourceMap",
"content": [
{
"element": "array",
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 21
},
"column": {
"element": "number",
"content": 11
}
},
"content": 414
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 21
},
"column": {
"element": "number",
"content": 17
}
},
"content": 6
}
]
}
]
}
]
}
},
"content": "'Parameter Object' contains unsupported key 'schema'"
},
{
"element": "annotation",
"meta": {
Expand Down Expand Up @@ -2047,66 +1987,6 @@
},
"content": "'Operation Object' contains unsupported key 'tags'"
},
{
"element": "annotation",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "warning"
}
]
}
},
"attributes": {
"sourceMap": {
"element": "array",
"content": [
{
"element": "sourceMap",
"content": [
{
"element": "array",
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 62
},
"column": {
"element": "number",
"content": 9
}
},
"content": 1504
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 62
},
"column": {
"element": "number",
"content": 15
}
},
"content": 6
}
]
}
]
}
]
}
},
"content": "'Parameter Object' contains unsupported key 'schema'"
},
{
"element": "annotation",
"meta": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,22 @@ describe('Parameter Object', () => {
bar: 'baz',
});
});

it('attaches integer example to member of schema object', () => {
const parameter = new namespace.elements.Object({
name: 'schema_example',
in: 'query',
schema: { type: 'string', example: 10 },
});

const parseResult = parse(context, parameter);

console.log(parseResult.get(0));

expect(parseResult.length).to.equal(1);
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Member);
expect(parseResult.get(0).value.toValue()).to.equal(10);
});
});

describe('#explode', () => {
Expand Down Expand Up @@ -518,18 +534,6 @@ describe('Parameter Object', () => {
expect(parseResult).to.contain.warning("'Parameter Object' contains unsupported key 'allowReserved'");
});

it('provides warning for unsupported schema property', () => {
const parameter = new namespace.elements.Object({
name: 'example',
in: 'query',
schema: { type: 'string' },
});

const parseResult = parse(context, parameter);

expect(parseResult).to.contain.warning("'Parameter Object' contains unsupported key 'schema'");
});

it('provides warning for unsupported examples property', () => {
const parameter = new namespace.elements.Object({
name: 'direction',
Expand Down