Skip to content

Commit

Permalink
validate if server object is empty in an openapi definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMario committed Nov 24, 2019
1 parent 9c4c57f commit bccb2c7
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/framework/util.ts
Expand Up @@ -51,7 +51,7 @@ export function sortApiDocTags(apiDoc) {
export function getBasePathsFromServers(
servers: OpenAPIV3.ServerObject[],
): BasePath[] {
if (!servers) {
if (!servers || servers.length == 0) {
return [new BasePath({ url: '' })];
}
const basePathsMap: { [key: string]: BasePath } = {};
Expand Down
36 changes: 36 additions & 0 deletions test/empty.servers.spec.ts
@@ -0,0 +1,36 @@
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');

describe(packageJson.name, () => {
let app = null;

before(async () => {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'empty.servers.yaml');
app = await createApp({ apiSpec }, 3007, app =>
app.use(
``,
express
.Router()
.get(`/pets`, (req, res) => res.json(req.body))
),
);
});

after(() => {
app.server.close();
});

it('should throw 400', async () =>
request(app)
.get(`/pets`)
.expect(400)
.then(r => {
console.log(r)
}));
});
214 changes: 214 additions & 0 deletions test/resources/empty.servers.yaml
@@ -0,0 +1,214 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API
termsOfService: http://swagger.io/terms/
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
servers: []
paths:
/pets:
get:
description: |
Returns all pets
operationId: findPets
parameters:
- name: type
in: query
description: maximum number of results to return
required: true
schema:
type: string
enum:
- dog
- cat
- name: tags
in: query
description: tags to filter by
required: false
style: form
schema:
type: array
items:
type: string
- name: limit
in: query
description: maximum number of results to return
required: true
schema:
type: integer
format: int32
minimum: 1
maximum: 20
responses:
'200':
description: pet response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

post:
description: Creates a new pet in the store. Duplicates are allowed
operationId: addPet
security:
- ApiKeyAuth: []
requestBody:
description: Pet to add to the store
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/NewPet'
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

/pets/{id}:
get:
description: Returns a user based on a single ID, if the user does not have access to the pet
operationId: find pet by id
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
schema:
type: integer
format: int64
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
delete:
description: deletes a single pet based on the ID supplied
operationId: deletePet
parameters:
- name: id
in: path
description: ID of pet to delete
required: true
schema:
type: integer
format: int64
responses:
'204':
description: pet deleted
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

/pets/{id}/photos:
post:
description: upload a photo of the pet
operationId: uploadPetPhoto
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
schema:
type: integer
format: int64
requestBody:
content:
multipart/form-data:
schema:
# $ref: '#/components/schemas/NewPhoto'
type: object
required:
- file
properties:
file:
description: The photo
type: string
format: binary
required: true
responses:
201:
description: Created
content:
application/json:
schema:
type: object
properties:
success:
type: boolean

components:
schemas:
NewPet:
required:
- name
properties:
name:
type: string
tag:
type: string
type:
$ref: '#/components/schemas/PetType'

Pet:
allOf:
- $ref: '#/components/schemas/NewPet'
- required:
- id
properties:
id:
type: integer
format: int64

PetType:
type: string
enum:
- dog
- cat

Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string

securitySchemes:
ApiKeyAuth:
type: apiKey
in: header
name: X-API-Key

0 comments on commit bccb2c7

Please sign in to comment.