Skip to content

Commit

Permalink
check example for array and nested object
Browse files Browse the repository at this point in the history
  • Loading branch information
coliu19 committed Mar 8, 2023
1 parent 6bace9e commit c952002
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 20 deletions.
48 changes: 30 additions & 18 deletions functions/ensureExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,16 @@ module.exports = function (targetVal) {
return;
}

if (targetVal.schema) {
if (targetVal.examples || targetVal.example || targetVal.schema.example) {
return;
}

if (targetVal.schema.properties) {
const props = targetVal.schema.properties;
let missing = false;
if (!targetVal.schema) {
return;
}

for (const key in props) {
if (props[key] && !props[key].example) {
missing = true;
break;
}
}
if (targetVal.examples || targetVal.example || targetVal.schema.example) {
return;
}

if (!missing) {
return;
}
}
if (hasExample(targetVal.schema)) {
return;
}

return [
Expand All @@ -56,3 +46,25 @@ module.exports = function (targetVal) {
},
];
};

function hasExample(target) {
if (target == null || target.examples || target.example) {
return true;
}

if (target.type === 'array') {
return hasExample(target.items);
}

if (!target.type || target.type === 'object') {
for (const key in target.properties) {
if (!hasExample(target.properties[key])) {
return false;
}
}

return true;
}

return false;
}
52 changes: 50 additions & 2 deletions test/examples-for-every-schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,54 @@ describe(ruleName, () => {
},
severity: 1,
},
{
code: ruleName,
message: 'For every schema provided in the OAS document, at least one example must be present; example or examples is missing in the object',
path: [
'paths',
'/test',
'get',
'responses',
'400',
'content',
'application/json',
],
range: {
end: {
character: 61,
line: 79,
},
start: {
character: 33,
line: 77,
},
},
severity: 1,
},
{
code: ruleName,
message: 'For every schema provided in the OAS document, at least one example must be present; example or examples is missing in the object',
path: [
'paths',
'/test',
'get',
'responses',
'401',
'content',
'application/json',
],
range: {
end: {
character: 63,
line: 91,
},
start: {
character: 33,
line: 87,
},
},
severity: 1,
},
{
code: ruleName,
message: 'For every schema provided in the OAS document, at least one example must be present; example or examples is missing in the object',
Expand All @@ -97,11 +145,11 @@ describe(ruleName, () => {
range: {
end: {
character: 38,
line: 137,
line: 160,
},
start: {
character: 33,
line: 124,
line: 147,
},
},
severity: 1,
Expand Down
59 changes: 59 additions & 0 deletions test/resources/examples-for-every-schema/negative.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@
}
}
},
"400": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AnotherError"
}
}
}
},
"401": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/AnotherError"
}
}
}
}
},
"500": {
"description": "unexpected error",
"content": {
Expand Down Expand Up @@ -202,6 +225,42 @@
"code": 2,
"message": "m"
}
},
"AnotherError": {
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32",
"example": 3
},
"message": {
"type": "string",
"example": "mm"
},
"errors": {
"type": "object",
"example": {
"a": 1
}
},
"details": {
"type": "object",
"properties": {
"a": {
"type": "integer",
"format": "int32",
"example": 3
},
"b": {
"type": "string"
}
}
}
}
}
},
"securitySchemes": {
Expand Down
60 changes: 60 additions & 0 deletions test/resources/examples-for-every-schema/positive.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@
}
}
},
"400": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AnotherError"
}
}
}
},
"401": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/AnotherError"
}
}
}
}
},
"500": {
"description": "unexpected error",
"content": {
Expand Down Expand Up @@ -248,6 +271,43 @@
"code": 2,
"message": "m"
}
},
"AnotherError": {
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32",
"example": 3
},
"message": {
"type": "string",
"example": "mm"
},
"errors": {
"type": "object",
"example": {
"a": 1
}
},
"details": {
"type": "object",
"properties": {
"a": {
"type": "integer",
"format": "int32",
"example": 3
},
"b": {
"type": "string",
"example": "s"
}
}
}
}
}
},
"securitySchemes": {
Expand Down

0 comments on commit c952002

Please sign in to comment.