add example of using examples in swagger 2.0

add example of using examples in swagger 2.0
This commit is contained in:
Luis Tejeda
2022-06-02 13:10:34 -05:00
parent 21190fa50b
commit d2e2be3c06
7 changed files with 191 additions and 3 deletions

View File

@@ -28,6 +28,7 @@ module.exports = {
* @param {array} traceFromParent - The trace from the parent key
* @param {string} filePathName - The filePath name from the file
* @param {string} localPart - The local path part
* @param {function} jsonPointerDecodeAndReplace - Function to decode a json pointer
* @returns {array} The trace to the container key
*/
getKeyInComponents30: function (traceFromParent, filePathName, localPart, jsonPointerDecodeAndReplace) {

View File

@@ -7,7 +7,8 @@ const { COMPONENTS_KEYS_30 } = require('./30XUtils/componentsParentMatcher'),
removeLocalReferenceFromPath,
localPointer,
jsonPointerLevelSeparator,
isLocalRef
isLocalRef,
jsonPointerDecodeAndReplace
} = require('./jsonPointer'),
traverseUtility = require('traverse'),
parse = require('./parse.js'),
@@ -127,7 +128,9 @@ function getContentFromTrace(content, partial) {
return content;
}
partial = partial[0] === jsonPointerLevelSeparator ? partial.substring(1) : partial;
const trace = partial.split(jsonPointerLevelSeparator);
const trace = partial.split(jsonPointerLevelSeparator).map((item) => {
return jsonPointerDecodeAndReplace(item);
});
let currentValue = content;
currentValue = deref._getEscaped(content, trace, undefined);
return currentValue;

View File

@@ -9,6 +9,9 @@ const COMPONENTS_KEYS_20 = [
'items',
'allOf',
'additionalProperties'
],
INLINE = [
'examples'
];
module.exports = {
@@ -17,6 +20,7 @@ module.exports = {
* @param {array} traceFromParent - The trace from the parent key
* @param {string} filePathName - The filePath name from the file
* @param {string} localPart - The local path part
* @param {function} jsonPointerDecodeAndReplace - Function to decode a json pointer
* @returns {array} The trace to the container key
*/
getKeyInComponents20: function (traceFromParent, filePathName, localPart, jsonPointerDecodeAndReplace) {
@@ -33,6 +37,10 @@ module.exports = {
if (SCHEMA_PARENTS.includes(item)) {
item = 'definitions';
}
if (INLINE.includes(item)) {
matchFound = false;
break;
}
traceToKey.push(item);
if (COMPONENTS_KEYS_20.includes(item)) {
matchFound = true;

View File

@@ -0,0 +1,2 @@
application/json:
name: Puma

View File

@@ -0,0 +1,88 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Swagger API Team",
"email": "apiteam@swagger.io",
"url": "http://swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"paths": {
"/pets": {
"get": {
"description": "Returns all pets alesuada ac...",
"operationId": "findPets",
"responses": {
"200": {
"description": "pet response",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Pet"
}
},
"examples": {
"application/json": {
"name": "Puma"
}
}
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/Error"
},
"examples": {
"application/json": {
"name": "Puma"
}
}
}
}
}
}
},
"definitions": {
"Pet": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"Error": {
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}

View File

@@ -0,0 +1,57 @@
swagger: '2.0'
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification
termsOfService: http://swagger.io/terms/
contact:
name: Swagger API Team
email: apiteam@swagger.io
url: http://swagger.io
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
paths:
/pets:
get:
description: Returns all pets alesuada ac...
operationId: findPets
responses:
'200':
description: pet response
schema:
type: array
items:
$ref: '#/definitions/Pet'
examples:
$ref: "examples.yaml"
default:
description: unexpected error
schema:
$ref: '#/definitions/Error'
examples:
application/json:
$ref: "examples.yaml#/application~1json"
definitions:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string

View File

@@ -40,7 +40,8 @@ let expect = require('chai').expect,
'/bringLocalDependenciesFromExternalMultiple'),
multipleRefFromRootComponents = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/multipleRefFromRootComponents'),
sameRefDifferentSource = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/sameRefDifferentSource'),
simpleRef = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/simpleRef');
simpleRef = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/simpleRef'),
refExample20 = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/referenced_example');
describe('bundle files method - 3.0', function () {
it('Should return bundled file as json - schema_from_response', async function () {
@@ -1335,6 +1336,34 @@ describe('bundle files method - 2.0', function() {
expect(res.result).to.be.true;
expect(res.output.data.bundledContent).to.be.equal(expected);
});
it('Should return bundled file with referenced example', async function () {
let contentRootFile = fs.readFileSync(refExample20 + '/root.yaml', 'utf8'),
examples = fs.readFileSync(refExample20 + '/examples.yaml', 'utf8'),
expected = fs.readFileSync(refExample20 + '/expected.json', 'utf8'),
input = {
type: 'folder',
specificationVersion: '2.0',
rootFiles: [
{
path: '/root.yaml',
content: contentRootFile
}
],
data: [
{
path: '/examples.yaml',
content: examples
}
],
options: {},
bundleFormat: 'JSON'
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data.bundledContent).to.be.equal(expected);
});
});
describe('getReferences method when node does not have any reference', function() {