Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for oneOf #104 #105

Merged
merged 8 commits into from
Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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