Force example to be inline

This commit is contained in:
Luis Tejeda
2022-07-07 15:26:12 -05:00
parent e7b91f48aa
commit dd7d1426c1
9 changed files with 359 additions and 19 deletions

View File

@@ -13,7 +13,8 @@ const SCHEMA_CONTAINERS = [
},
INLINE = [
'examples',
'value'
'value',
'example'
],
COMPONENTS_KEYS = [
'definitions',

View File

@@ -8,7 +8,6 @@ const SCHEMA_CONTAINERS = [
'schema'
],
EXAMPLE_CONTAINERS = [
'example'
],
REQUEST_BODY_CONTAINER = [
'requestBody'
@@ -23,11 +22,13 @@ const SCHEMA_CONTAINERS = [
responses: 'responses',
callbacks: 'callbacks',
properties: 'schemas',
links: 'links'
links: 'links',
examples: 'examples'
},
INLINE = [
'properties',
'value'
'value',
'example'
],
ROOT_CONTAINERS_KEYS = [
'components'

View File

@@ -8,7 +8,6 @@ const SCHEMA_CONTAINERS = [
'schema'
],
EXAMPLE_CONTAINERS = [
'example'
],
REQUEST_BODY_CONTAINER = [
'requestBody'
@@ -24,11 +23,13 @@ const SCHEMA_CONTAINERS = [
callbacks: 'callbacks',
properties: 'schemas',
links: 'links',
paths: 'pathItems'
paths: 'pathItems',
examples: 'examples'
},
INLINE = [
'properties',
'value'
'value',
'example'
],
ROOT_CONTAINERS_KEYS = [
'components'

View File

@@ -88,7 +88,7 @@ function getKeyInComponents(traceFromParent, mainKey, version, commonPathFromDat
}
item = resolveFirstLevelChild(item, CONTAINERS);
resolveSecondLevelChild(trace, index, DEFINITIONS);
traceToKey.push(item);
traceToKey.push(item.replace(/\s/g, ''));
if (COMPONENTS_KEYS.includes(item)) {
matchFound = true;
break;

View File

@@ -0,0 +1,106 @@
openapi: "3.0.0"
info:
version: 1.0.10
title: Petstore 224
description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
termsOfService: http://swagger.io/terms/
contact:
name: Postman
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
x-postman-projectname: PostmanPetstore
paths:
/pets:
parameters:
- name: GlobalParam
in: query
schema:
type: string
- name: GlobalCookie
in: cookie
schema:
type: string
post:
description: Creates a new pet in the store. Duplicates are allowed
operationId: addPet
requestBody:
description: Pet to add to the store
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
examples:
Single Droplet Create Request:
$ref: "./example2.yaml#/droplet_create_request"
components:
schemas:
Pet:
type: object
description: A pet
required:
- name
properties:
id:
type: integer
format: int64
name:
type: string
typeOf:
type: string
enum: [Cat, Dog, Bird, Reptile]
tag:
type: string
Pets:
type: object
properties:
id:
type: integer
StoreItem:
description: Single store item
properties:
id:
type: integer
format: int64
name:
type: string
typeOf:
type: string
enum: [ Cat, Dog, Bird, Reptile ]
tag:
type: string
value:
type: number
format: float
description: price
saleValue:
type: number
format: float
description: price if on sale
onSale:
type: boolean
default: false
description: True if item is on sale, false if not.
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string

View File

@@ -0,0 +1,17 @@
droplet_create_request:
value:
name: example.com
region: nyc3
size: s-1vcpu-1gb
image: ubuntu-20-04-x64
ssh_keys:
- 289794
- 3b:16:e4:bf:8b:00:8b:b8:59:8c:a9:d3:f0:19:fa:45
backups: true
ipv6: true
monitoring: true
tags:
- env:prod
- web
user_data: "#cloud-config\nruncmd:\n - touch /test.txt\n"
vpc_uuid: 760e09ef-dc84-11e8-981e-3cfdfeaae000

View File

@@ -0,0 +1,187 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.10",
"title": "Petstore 224",
"description": "A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Postman"
},
"license": {
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"x-postman-projectname": "PostmanPetstore"
},
"paths": {
"/pets": {
"parameters": [
{
"name": "GlobalParam",
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "GlobalCookie",
"in": "cookie",
"schema": {
"type": "string"
}
}
],
"post": {
"description": "Creates a new pet in the store. Duplicates are allowed",
"operationId": "addPet",
"requestBody": {
"description": "Pet to add to the store",
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"responses": {
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
},
"examples": {
"Single Droplet Create Request": {
"$ref": "#/components/examples/_example2.yaml-_droplet_create_request"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"description": "A pet",
"required": [
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"typeOf": {
"type": "string",
"enum": [
"Cat",
"Dog",
"Bird",
"Reptile"
]
},
"tag": {
"type": "string"
}
}
},
"Pets": {
"type": "object",
"properties": {
"id": {
"type": "integer"
}
}
},
"StoreItem": {
"description": "Single store item",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"typeOf": {
"type": "string",
"enum": [
"Cat",
"Dog",
"Bird",
"Reptile"
]
},
"tag": {
"type": "string"
},
"value": {
"type": "number",
"format": "float",
"description": "price"
},
"saleValue": {
"type": "number",
"format": "float",
"description": "price if on sale"
},
"onSale": {
"type": "boolean",
"default": false,
"description": "True if item is on sale, false if not."
}
}
},
"Error": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
},
"examples": {
"_example2.yaml-_droplet_create_request": {
"value": {
"name": "example.com",
"region": "nyc3",
"size": "s-1vcpu-1gb",
"image": "ubuntu-20-04-x64",
"ssh_keys": [
289794,
"3b:16:e4:bf:8b:00:8b:b8:59:8c:a9:d3:f0:19:fa:45"
],
"backups": true,
"ipv6": true,
"monitoring": true,
"tags": [
"env:prod",
"web"
],
"user_data": "#cloud-config\nruncmd:\n - touch /test.txt\n",
"vpc_uuid": "760e09ef-dc84-11e8-981e-3cfdfeaae000"
}
}
}
}
}

View File

@@ -32,7 +32,11 @@
}
},
"example": {
"$ref": "#/components/examples/_examples.yaml-_foo"
"summary": "sum",
"value": {
"code": 1,
"message": "test error message"
}
}
}
}
@@ -86,15 +90,6 @@
}
}
}
},
"examples": {
"_examples.yaml-_foo": {
"summary": "sum",
"value": {
"code": 1,
"message": "test error message"
}
}
}
}
}

View File

@@ -46,7 +46,8 @@ let expect = require('chai').expect,
referencedComponents = path.join(__dirname, BUNDLES_FOLDER + '/referenced_components'),
referencedPath = path.join(__dirname, BUNDLES_FOLDER + '/referenced_path'),
referencedPathSchema = path.join(__dirname, BUNDLES_FOLDER + '/paths_schema'),
exampleValue = path.join(__dirname, BUNDLES_FOLDER + '/example_value');
exampleValue = path.join(__dirname, BUNDLES_FOLDER + '/example_value'),
example2 = path.join(__dirname, BUNDLES_FOLDER + '/example2');
describe('bundle files method - 3.0', function () {
it('Should return bundled file as json - schema_from_response', async function () {
@@ -2580,6 +2581,37 @@ describe('bundle files method - 3.0', function () {
expect(res.output.specification.version).to.equal('3.0');
expect(JSON.stringify(JSON.parse(res.output.data[0].bundledContent), null, 2)).to.be.equal(expected);
});
it('Should resolve examples correctly', async function () {
let contentRootFile = fs.readFileSync(example2 + '/another.yml', 'utf8'),
example = fs.readFileSync(example2 + '/example2.yaml', 'utf8'),
expected = fs.readFileSync(example2 + '/expected.json', 'utf8'),
input = {
type: 'multiFile',
specificationVersion: '3.0',
rootFiles: [
{
path: '/another.yml'
}
],
data: [
{
path: '/another.yml',
content: contentRootFile
},
{
path: '/example2.yaml',
content: example
}
],
options: {},
bundleFormat: 'JSON'
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.specification.version).to.equal('3.0');
expect(JSON.stringify(JSON.parse(res.output.data[0].bundledContent), null, 2)).to.be.equal(expected);
});
});
describe('getReferences method when node does not have any reference', function() {