Merging last swagger20Support with last version

This commit is contained in:
Erik Mendoza
2022-06-09 19:00:01 -05:00
24 changed files with 430 additions and 111 deletions

View File

@@ -12,6 +12,9 @@ const COMPONENTS_KEYS_20 = [
], ],
INLINE = [ INLINE = [
'examples' 'examples'
],
PROPERTY_DEFINITION = [
'properties'
]; ];
module.exports = { module.exports = {
@@ -28,7 +31,8 @@ module.exports = {
trace = [], trace = [],
traceToKey = [], traceToKey = [],
matchFound = false, matchFound = false,
isRootKey = false; isRootKey = false,
traceModified = false;
res.push(jsonPointerDecodeAndReplace(`${filePathName}${localPart}`)); res.push(jsonPointerDecodeAndReplace(`${filePathName}${localPart}`));
trace = [...res].reverse(); trace = [...res].reverse();
@@ -36,6 +40,11 @@ module.exports = {
for (let [index, item] of trace.entries()) { for (let [index, item] of trace.entries()) {
if (SCHEMA_PARENTS.includes(item)) { if (SCHEMA_PARENTS.includes(item)) {
item = 'definitions'; item = 'definitions';
traceModified = true;
}
if (PROPERTY_DEFINITION.includes(trace[index + 2])) {
trace[index + 1] = 'definitions';
traceModified = true;
} }
if (INLINE.includes(item)) { if (INLINE.includes(item)) {
matchFound = false; matchFound = false;
@@ -44,7 +53,8 @@ module.exports = {
traceToKey.push(item); traceToKey.push(item);
if (COMPONENTS_KEYS_20.includes(item)) { if (COMPONENTS_KEYS_20.includes(item)) {
matchFound = true; matchFound = true;
isRootKey = trace[index + 1] === undefined; isRootKey = trace[index + 1] === undefined &&
!traceModified;
break; break;
} }
} }

View File

@@ -11,4 +11,4 @@ properties:
country: country:
$ref: "./country.yaml" $ref: "./country.yaml"
warrior: warrior:
$ref: "./warrior.yaml" $ref: "./warrior.yaml"

View File

@@ -3,4 +3,4 @@ properties:
power: power:
type: string type: string
weapon: weapon:
type: string type: string

View File

@@ -0,0 +1,52 @@
{
"openapi": "3.0.0",
"info": {
"title": "Sample API",
"description": "Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.",
"version": "0.1.9"
},
"servers": [
{
"url": "http://api.example.com/v1",
"description": "Optional server description, e.g. Main (production) server"
},
{
"url": "http://staging-api.example.com",
"description": "Optional server description, e.g. Internal staging server for testing"
}
],
"paths": {
"/users/{userId}": {
"get": {
"summary": "Get a user by ID",
"responses": {
"200": {
"description": "A single user.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/~1schemas~1user.yaml"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"/schemas/user.yaml": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"userName": {
"type": "string"
}
}
}
}
}
}

View File

@@ -0,0 +1,32 @@
openapi: 3.0.0
info:
title: Sample API
description: >-
Optional multiline or single-line description in
[CommonMark](http://commonmark.org/help/) or HTML.
version: 0.1.9
servers:
- url: 'http://api.example.com/v1'
description: 'Optional server description, e.g. Main (production) server'
- url: 'http://staging-api.example.com'
description: 'Optional server description, e.g. Internal staging server for testing'
paths:
'/users/{userId}':
get:
summary: Get a user by ID
responses:
'200':
description: A single user.
content:
application/json:
schema:
$ref: '#/components/schemas/~1schemas~1user.yaml'
components:
schemas:
/schemas/user.yaml:
type: object
properties:
id:
type: integer
userName:
type: string

View File

@@ -0,0 +1,6 @@
headerTest:
schema:
type: integer
example: 5000
description: >-
The test header

View File

@@ -0,0 +1,26 @@
openapi: 3.0.0
info:
title: Sample API
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
version: 0.1.9
servers:
- url: http://api.example.com/v1
description: Optional server description, e.g. Main (production) server
- url: http://staging-api.example.com
description: Optional server description, e.g. Internal staging server for testing
paths:
/users/{userId}:
get:
summary: Get a user by ID
responses:
200:
description: A single user.
headers:
ratelimit-limit:
$ref: '../headers.yaml#/headerTest'
content:
application/json:
schema:
$ref: "./schemas/user.yaml"

View File

@@ -0,0 +1,6 @@
type: object
properties:
id:
type: integer
userName:
type: string

View File

@@ -51,18 +51,21 @@
"type": "string" "type": "string"
}, },
"Color": { "Color": {
"type": "object", "$ref": "#/definitions/~1pet.yaml%23~1Color"
"properties": { }
"name": { }
"type": "string" },
}, "/pet.yaml#/Color": {
"uses": { "type": "object",
"type": "string" "properties": {
}, "name": {
"color": { "type": "string"
"type": "string" },
} "uses": {
} "type": "string"
},
"color": {
"type": "string"
} }
} }
} }

View File

@@ -80,38 +80,47 @@
"type": "string" "type": "string"
}, },
"Color": { "Color": {
"type": "object", "$ref": "#/definitions/~1pet.yaml%23~1Color"
"properties": {
"name": {
"type": "string"
},
"uses": {
"type": "string"
},
"color": {
"type": "string"
}
}
}, },
"FavoriteFood": { "FavoriteFood": {
"type": "object", "$ref": "#/definitions/~1food.yaml%23~1Food"
"properties": {
"brand": {
"type": "string"
},
"benefits": {
"type": "array",
"items": {
"$ref": "#/definitions/~1food.yaml%23~1Benefit"
}
},
"cost": {
"type": "string"
}
}
} }
} }
}, },
"/pet.yaml#/Color": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"uses": {
"type": "string"
},
"color": {
"type": "string"
}
}
},
"/food.yaml#/Food": {
"type": "object",
"properties": {
"brand": {
"$ref": "#/definitions/~1food.yaml%23~1Brand"
},
"benefits": {
"type": "array",
"items": {
"$ref": "#/definitions/~1food.yaml%23~1Benefit"
}
},
"cost": {
"type": "string"
}
}
},
"/food.yaml#/Brand": {
"type": "string"
},
"/food.yaml#/Benefit": { "/food.yaml#/Benefit": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -52,18 +52,21 @@
"type": "string" "type": "string"
}, },
"Color": { "Color": {
"type": "object", "$ref": "#/definitions/~1pet.yaml%23~1Color"
"properties": { }
"name": { }
"type": "string" },
}, "/pet.yaml#/Color": {
"uses": { "type": "object",
"type": "string" "properties": {
}, "name": {
"color": { "type": "string"
"type": "string" },
} "uses": {
} "type": "string"
},
"color": {
"type": "string"
} }
} }
} }

View File

@@ -47,20 +47,26 @@
"type": "string" "type": "string"
}, },
"favoriteFood": { "favoriteFood": {
"type": "object", "$ref": "#/definitions/~1schemas~1favorite_food.yaml%23~1FavoriteFood"
"properties": {
"name": {
"type": "string"
},
"brand": {
"type": "string"
}
}
}, },
"foodPrice": { "foodPrice": {
"type": "integer" "$ref": "#/definitions/~1schemas~1favorite_food.yaml%23~1Price"
} }
} }
},
"/schemas/favorite_food.yaml#/FavoriteFood": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"brand": {
"type": "string"
}
}
},
"/schemas/favorite_food.yaml#/Price": {
"type": "integer"
} }
} }
} }

View File

@@ -0,0 +1,68 @@
{
"swagger": "2.0",
"info": {
"title": "Sample API",
"description": "API description in Markdown.",
"version": "1.0.0"
},
"host": "api.example.com",
"basePath": "/v1",
"schemes": [
"https"
],
"paths": {
"/users": {
"get": {
"summary": "Returns a list of users.",
"description": "Optional extended description in Markdown.",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/~1schemas~1user.yaml"
}
}
}
}
}
}
},
"definitions": {
"/schemas/user.yaml": {
"type": "object",
"properties": {
"age": {
"$ref": "#/definitions/~1schemas~1age.yaml"
},
"hobbies": {
"$ref": "#/definitions/~1schemas~1hobbies.yaml"
}
}
},
"/schemas/age.yaml": {
"type": "string"
},
"/schemas/hobbies.yaml": {
"type": "array",
"items": {
"$ref": "#/definitions/~1schemas~1hobby.yaml"
}
},
"/schemas/hobby.yaml": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"position": {
"type": "integer"
}
}
}
}
}

View File

@@ -0,0 +1,9 @@
swagger: "2.0"
info:
$ref: ./info.yaml
host: api.example.com
basePath: /v1
schemes:
- https
paths:
$ref: ./paths.yaml

View File

@@ -0,0 +1,3 @@
title: Sample API
description: API description in Markdown.
version: 1.0.0

View File

@@ -0,0 +1,13 @@
/users:
get:
summary: Returns a list of users.
description: Optional extended description in Markdown.
produces:
- application/json
responses:
200:
description: OK
schema:
type: array
items:
$ref: "./schemas/user.yaml"

View File

@@ -0,0 +1 @@
type: string

View File

@@ -0,0 +1,3 @@
type: array
items:
$ref: "./hobby.yaml"

View File

@@ -0,0 +1,6 @@
type: object
properties:
name:
type: string
position:
type: integer

View File

@@ -0,0 +1,6 @@
type: object
properties:
age:
$ref: "./age.yaml"
hobbies:
$ref: "./hobbies.yaml"

View File

@@ -47,15 +47,18 @@
"type": "string" "type": "string"
}, },
"favoriteFood": { "favoriteFood": {
"type": "object", "$ref": "#/definitions/~1schemas~1favorite_food.yaml"
"properties": { }
"name": { }
"type": "string" },
}, "/schemas/favorite_food.yaml": {
"brand": { "type": "object",
"type": "string" "properties": {
} "name": {
} "type": "string"
},
"brand": {
"type": "string"
} }
} }
} }

View File

@@ -22,7 +22,7 @@
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/~1schemas~1user.yaml" "$ref": "#/definitions/~1schemas~1user.yaml%23User"
} }
} }
} }
@@ -39,7 +39,7 @@
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/~1otherSchemas~1client.yaml" "$ref": "#/definitions/~1otherSchemas~1client.yaml%23Client"
} }
} }
} }
@@ -47,42 +47,38 @@
} }
}, },
"definitions": { "definitions": {
"/schemas/user.yaml": { "/schemas/user.yaml#User": {
"User": { "type": "object",
"type": "object", "required": [
"required": [ "id",
"id", "name"
"name" ],
], "properties": {
"properties": { "id": {
"id": { "type": "integer"
"type": "integer" },
}, "detail": {
"detail": { "type": "array",
"type": "array", "items": {
"items": { "$ref": "#/definitions/~1schemas~1detail.yaml%23~1Detail"
"$ref": "#/definitions/~1schemas~1detail.yaml%23~1Detail"
}
} }
} }
} }
}, },
"/otherSchemas/client.yaml": { "/otherSchemas/client.yaml#Client": {
"Client": { "type": "object",
"type": "object", "required": [
"required": [ "id",
"id", "name"
"name" ],
], "properties": {
"properties": { "id": {
"id": { "type": "integer"
"type": "integer" },
}, "details": {
"details": { "type": "array",
"type": "array", "items": {
"items": { "$ref": "#/definitions/~1otherSchemas~1detail.yaml%23~1Detail"
"$ref": "#/definitions/~1otherSchemas~1detail.yaml%23~1Detail"
}
} }
} }
} }

View File

@@ -8,7 +8,7 @@
200: 200:
description: OK description: OK
schema: schema:
$ref: "./schemas/user.yaml" $ref: "./schemas/user.yaml#User"
/clients: /clients:
get: get:
summary: Returns a list of users. summary: Returns a list of users.
@@ -19,4 +19,4 @@
200: 200:
description: OK description: OK
schema: schema:
$ref: "./otherSchemas/client.yaml" $ref: "./otherSchemas/client.yaml#Client"

View File

@@ -40,13 +40,13 @@ let expect = require('chai').expect,
'/bringLocalDependenciesFromExternalMultiple'), '/bringLocalDependenciesFromExternalMultiple'),
multipleRefFromRootComponents = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/multipleRefFromRootComponents'), multipleRefFromRootComponents = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/multipleRefFromRootComponents'),
sameRefDifferentSource = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/sameRefDifferentSource'), sameRefDifferentSource = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/sameRefDifferentSource'),
nestedProperties20 = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/nestedProperties20'),
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'), refExample20 = path.join(__dirname, SWAGGER_MULTIFILE_FOLDER + '/referenced_example'),
properties = path.join(__dirname, BUNDLES_FOLDER + '/properties'), properties = path.join(__dirname, BUNDLES_FOLDER + '/properties'),
sameSourceDifferentPlace = path.join(__dirname, BUNDLES_FOLDER + '/same_source_different_place'), sameSourceDifferentPlace = path.join(__dirname, BUNDLES_FOLDER + '/same_source_different_place'),
nestedProperties = path.join(__dirname, BUNDLES_FOLDER + '/nestedProperties'); nestedProperties = path.join(__dirname, BUNDLES_FOLDER + '/nestedProperties');
describe('bundle files method - 3.0', function () { describe('bundle files method - 3.0', function () {
it('Should return bundled file as json - schema_from_response', async function () { it('Should return bundled file as json - schema_from_response', async function () {
let contentRootFile = fs.readFileSync(schemaFromResponse + '/root.yaml', 'utf8'), let contentRootFile = fs.readFileSync(schemaFromResponse + '/root.yaml', 'utf8'),
@@ -997,6 +997,8 @@ describe('bundle files method - 3.0', function () {
expect(res.result).to.be.true; expect(res.result).to.be.true;
expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected); expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected);
expect(JSON.stringify(res.output.data[1].bundledContent, null, 2)).to.be.equal(expected2); expect(JSON.stringify(res.output.data[1].bundledContent, null, 2)).to.be.equal(expected2);
expect(res.output.data[0].rootFile.path).to.equal('/root.yaml');
expect(res.output.data[1].rootFile.path).to.equal('/root2.yaml');
}); });
it('Should throw error when root is not present in data array', async function () { it('Should throw error when root is not present in data array', async function () {
@@ -1492,6 +1494,62 @@ describe('bundle files method - 3.0', function () {
}); });
describe('bundle files method - 2.0', function() { describe('bundle files method - 2.0', function() {
it('Should return bundled result from - nestedProperties20', async function() {
let contentRootFile = fs.readFileSync(nestedProperties20 + '/index.yaml', 'utf8'),
info = fs.readFileSync(nestedProperties20 + '/info.yaml', 'utf8'),
paths = fs.readFileSync(nestedProperties20 + '/paths.yaml', 'utf8'),
age = fs.readFileSync(nestedProperties20 + '/schemas/age.yaml', 'utf8'),
hobbies = fs.readFileSync(nestedProperties20 + '/schemas/hobbies.yaml', 'utf8'),
hobby = fs.readFileSync(nestedProperties20 + '/schemas/hobby.yaml', 'utf8'),
user = fs.readFileSync(nestedProperties20 + '/schemas/user.yaml', 'utf8'),
expected = fs.readFileSync(nestedProperties20 + '/bundleExpected.json', 'utf8'),
input = {
type: 'multiFile',
specificationVersion: '2.0',
rootFiles: [
{
path: '/index.yaml'
}
],
data: [
{
path: '/index.yaml',
content: contentRootFile
},
{
path: '/info.yaml',
content: info
},
{
path: '/paths.yaml',
content: paths
},
{
path: '/schemas/user.yaml',
content: user
},
{
path: '/schemas/age.yaml',
content: age
},
{
path: '/schemas/hobbies.yaml',
content: hobbies
},
{
path: '/schemas/hobby.yaml',
content: hobby
}
],
options: {},
bundleFormat: 'JSON'
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected);
});
it('Should return bundled result from - sameRefDifferentSource', async function() { it('Should return bundled result from - sameRefDifferentSource', async function() {
let contentRootFile = fs.readFileSync(sameRefDifferentSource + '/index.yaml', 'utf8'), let contentRootFile = fs.readFileSync(sameRefDifferentSource + '/index.yaml', 'utf8'),
info = fs.readFileSync(sameRefDifferentSource + '/info.yaml', 'utf8'), info = fs.readFileSync(sameRefDifferentSource + '/info.yaml', 'utf8'),