Skip to content

Commit

Permalink
Merge pull request #105 from cdimascio/feature
Browse files Browse the repository at this point in the history
add test for oneOf #104
  • Loading branch information
cdimascio authored Nov 3, 2019
2 parents 8ee7760 + 5d72556 commit 86eb3c4
Show file tree
Hide file tree
Showing 2 changed files with 309 additions and 0 deletions.
148 changes: 148 additions & 0 deletions test/one.of.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
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 () => {
const apiSpec = path.join('test', 'resources', 'one.of.yaml');
app = await createApp(
{ apiSpec },
3005,
app => {
app.post(`${app.basePath}/one_of`, (req, res) => {
res.json(req.body);
});
app.post(`${app.basePath}/one_of_b`, (req, res) => {
res.json(req.body);
});
app.use((err, req, res, next) => {
res.status(err.status || 500).json({
message: err.message,
code: err.status || 500,
});
});
},
false,
);
});

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

it('should return 200 one first oneOf option', async () => {
return request(app)
.post(`${app.basePath}/one_of`)
.set('content-type', 'application/json')
.send({
id: 'some_id',
array_of_oneofs: [
{
type: 'type_1',
unique_one: 'unique_one',
},
],
})
.expect(200);
});

it('should return 200 one second oneOf option', async () => {
return request(app)
.post(`${app.basePath}/one_of`)
.set('content-type', 'application/json')
.send({
id: 'some_id',
array_of_oneofs: [
{
type: 'type_2',
unique_two: 'unique_two',
},
],
})
.expect(200);
});

it('should return 400 for invalid oneOf option', async () => {
return request(app)
.post(`${app.basePath}/one_of`)
.set('content-type', 'application/json')
.send({
id: 'some_id',
array_of_oneofs: [
{
type: 'type_2',
unique_three: 'unique_three',
},
],
})
.expect(400)
.then(r => {
const e = r.body;
expect(e.message).to.contain(
'should match exactly one schema in oneOf',
);
});
});

it('should return 200 on first oneOf (b) option', async () => {
return request(app)
.post(`${app.basePath}/one_of_b`)
.set('content-type', 'application/json')
.send({
id: 'some_id',
array_of_oneofs: [
{
type: 'type_1',
unique_one: 'unique_one',
value: 1
},
],
})
.expect(200);
});

it('should return 200 on second oneOf (b) option', async () => {
return request(app)
.post(`${app.basePath}/one_of_b`)
.set('content-type', 'application/json')
.send({
id: 'some_id',
array_of_oneofs: [
{
type: 'type_2',
unique_two: 'unique_two',
value: 2
},
],
})
.expect(200);
});

it('should return 400 for invalid oneOf (b) option', async () => {
return request(app)
.post(`${app.basePath}/one_of_b`)
.set('content-type', 'application/json')
.send({
id: 'some_id',
array_of_oneofs: [
{
type: 'type_2',
unique_three: 'unique_three',
},
],
})
.expect(400)
.then(r => {
const e = r.body;
expect(e.message).to.contain(
'should match exactly one schema in oneOf',
);
});
});
});
161 changes: 161 additions & 0 deletions test/resources/one.of.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
openapi: "3.0.2"
info:
version: 1.0.0
title: requestBodies $ref
description: requestBodies $ref Test

servers:
- url: /v1

paths:
/one_of:
post:
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/OneOfType"
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/OneOfType'
"400":
description: Bad Request

/one_of_b:
post:
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/OneOfTypeB"
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/OneOfType'
"400":
description: Bad Request

components:
schemas:
SimilarTypeOne:
allOf:
- $ref: "#/components/schemas/SimilarSuperType"
- type: object
properties:
type:
type: string
enum:
- type_1
unique_one:
type: string
required:
- type
- unique_one

SimilarTypeTwo:
allOf:
- $ref: "#/components/schemas/SimilarSuperType"
- type: object
properties:
type:
type: string
enum:
- type_2
unique_two:
type: string
required:
- type
- unique_two

OneOfType:
type: object
properties:
array_of_oneofs:
type: array
items:
oneOf:
- $ref: "#/components/schemas/SimilarTypeOne"
- $ref: "#/components/schemas/SimilarTypeTwo"
discriminator:
propertyName: type

SimilarTypeOneB:
allOf:
- $ref: "#/components/schemas/SimilarSuperTypeB"
properties:
type:
type: string
enum:
- type_1
unique_one:
type: string
value:
type: number
format: int32
enum: [ 1 ]
default: 1
required:
- type
- unique_one
- value

SimilarTypeTwoB:
allOf:
- $ref: "#/components/schemas/SimilarSuperTypeB"
properties:
type:
type: string
enum:
- type_2
unique_two:
type: string
value:
type: number
format: int32
enum: [ 2 ]
default: 2
required:
- type
- unique_two
- value

OneOfTypeB:
type: object
properties:
array_of_oneofs:
type: array
items:
oneOf:
- $ref: "#/components/schemas/SimilarTypeOneB"
- $ref: "#/components/schemas/SimilarTypeTwoB"
discriminator:
propertyName: type

SimilarSuperType:
type: object
properties:
id:
type: string

SimilarSuperTypeB:
type: object
properties:
id:
type: string
value:
type: number
format: int32
discriminator:
propertyName: type
required:
- value

0 comments on commit 86eb3c4

Please sign in to comment.