Skip to content

Commit

Permalink
Merge pull request #46 from cdimascio/nullable
Browse files Browse the repository at this point in the history
Reject requests with null value if "nullable" is not set #45
  • Loading branch information
cdimascio committed Aug 30, 2019
2 parents af4da80 + 34e154a commit 17dfc20
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-openapi-validator",
"version": "1.6.2",
"version": "1.7.0",
"description": "Automatically validate API requests using an OpenAPI 3 and Express.",
"main": "dist/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class OpenApiValidator {
}

const aoav = new middlewares.RequestValidator(this.context.apiDoc, {
nullable: true,
coerceTypes: true,
removeAdditional: false,
useDefaults: true,
Expand Down
5 changes: 5 additions & 0 deletions test/common/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as path from 'path';
import { createApp } from '../common/app';

const apiSpec = path.join('test', 'resources', 'openapi.yaml');
createApp({ apiSpec }, 3000);
52 changes: 52 additions & 0 deletions test/nullable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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;
let basePath = null;

before(async () => {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'nullable.yaml');
app = await createApp({ apiSpec }, 3005);
basePath = app.basePath;

app.use(
`${basePath}`,
express
.Router()
.post(`/pets/nullable`, (req, res) => res.json(req.body)),
);
});

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

it('should allow null to be set (name: nullable true)', async () =>
request(app)
.post(`${basePath}/pets/nullable`)
.send({
name: null,
})
.expect(200)
.then(r => {
expect(r.body.name).to.be.null;
}));

it('should fill null with default (name: nullable false/default)', async () =>
request(app)
.post(`${basePath}/pets`)
.send({
name: null,
})
.expect(200)
.then(r => {
expect(r.body.name).to.equal('');
}));
});
113 changes: 113 additions & 0 deletions test/resources/nullable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
openapi: '3.0.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 OpenAPI 3.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
servers:
- url: /v1/
- url: http://{name}.swagger.io:{port}/{version}
variables:
name:
default: petstore
enum:
- petstore
- storeofpets
port:
enum:
- '443'
- '8443'
default: '443'
version:
default: v1
enum:
- v1
paths:
/pets/nullable:
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/NullableNewPet'
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'

/pets:
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/NewPet'
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'

components:
parameters:
id:
name: id
in: path
description: ID of pet to fetch
required: true
schema:
type: integer
format: int64

schemas:
NullableNewPet:
additionalProperties: false
required:
- name
properties:
name:
type: string
nullable: true
tag:
type: string

NewPet:
additionalProperties: false
required:
- name
properties:
name:
type: string
tag:
type: string


Pet:
allOf:
- $ref: '#/components/schemas/NewPet'
- required:
- id
properties:
id:
type: integer
format: int64
1 change: 1 addition & 0 deletions test/resources/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ components:
properties:
name:
type: string
nullable: true
tag:
type: string

Expand Down

0 comments on commit 17dfc20

Please sign in to comment.