Skip to content

Commit

Permalink
add oneOf test
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Nov 3, 2019
1 parent 8ee7760 commit f213ffc
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
89 changes: 89 additions & 0 deletions test/one.of.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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.only(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.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',
);
});
});
});
89 changes: 89 additions & 0 deletions test/resources/one.of.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
openapi: "3.0.2"
info:
version: 1.0.0
title: requestBodies $ref
description: requestBodies $ref Test

servers:
- url: /v1

paths:
/one_of:
post:
requestBody:
# $ref: "#/components/requestBodies/OneOfBody"
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

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

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

# requestBodies:
# OneOfBody:
# required: true
# content:
# text/plain:
# schema:
# type: string
# application/json:
# schema:
# $ref: "#/components/schemas/OneOfType"

0 comments on commit f213ffc

Please sign in to comment.