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

WIP: create second set of oneOf tests (using @catadch's example) #107

Closed
wants to merge 8 commits into from
Closed
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
89 changes: 89 additions & 0 deletions test/one.of.discriminator.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.2.yaml');
app = await createApp(
{ apiSpec },
3005,
app => {
app.post(`${app.basePath}/typethrees`, (req, res) => {
res.status(201).json(req.body);
});
app.use((err, req, res, next) => {
console.log(err);
res.status(err.status || 500).json({
message: err.message,
code: err.status || 500,
});
});
},
false,
);
});

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

it('should POST TypeOne', async () => {
return request(app)
.post(`${app.basePath}/typethrees`)
.send({
whatever: 'Whatever One',
somethings: [
{
type: 'TypeOne',
uniqueOne: 'Unique One',
// value: 1
},
],
})
.expect(201)
.expect('Content-Type', /json/)
.then(data => {
expect(data).to.not.be.an('undefined');
expect(data.body).to.not.be.an('undefined');
expect(data.body.whatever).to.not.be.an('undefined');
expect(data.body.whatever).to.equal('Whatever One');
expect(data.body.somethings).to.not.be.an('undefined');
expect(data.body.somethings[0].type).to.equal('TypeOne');
expect(data.body.somethings[0].uniqueOne).to.equal('Unique One');
});
});

// TODO make this work - needs to utilize a discriminator
it('should POST TypeTwo', async () => {
return request(app)
.post(`${app.basePath}/typethrees`)
.send({
whatever: 'Whatever Two',
somethings: [
{
type: 'TypeTwo',
uniqueTwo: 'Unique Two',
// value: 2 // make it work
},
],
})
.expect(201)
.expect('Content-Type', /json/)
.then(data => {
expect(data).to.not.be.an('undefined');
expect(data.body).to.not.be.an('undefined');
expect(data.body.whatever).to.not.be.an('undefined');
expect(data.body.whatever).to.equal('Whatever Two');
expect(data.body.somethings).to.not.be.an('undefined');
expect(data.body.somethings[0].type).to.equal('TypeTwo');
expect(data.body.somethings[0].uniqueTwo).to.equal('Unique Two');
});
});
});
139 changes: 139 additions & 0 deletions test/resources/one.of.discriminator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
openapi: "3.0.0"

info:
description: express-openapi-validator-test
version: 1.0.0
title: express-openapi-validator-test
license:
name: MIT License
url: https://opensource.org/licenses/MIT

servers:
- url: /v1

paths:
/typethrees:
post:
requestBody:
required: true
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/TypeThree"
responses:
"201":
description: Application response
content:
application/json:
schema:
$ref: "#/components/schemas/TypeThree"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"

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

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

TypeOne:
allOf:
- $ref: "#/components/schemas/SuperTypeOne"
type: object
properties:
type:
type: string
enum: [TypeOne]
value:
type: number
format: int32
enum: [1]
default: 1
uniqueOne:
type: string
default: Unique One
required:
- type
- uniqueOne

TypeTwo:
allOf:
- $ref: "#/components/schemas/SuperTypeOne"
type: object
properties:
type:
type: string
enum: [TypeTwo]
value:
type: number
format: int32
enum: [2]
default: 2
uniqueTwo:
type: string
default: Unique Two
required:
- uniqueTwo

SuperTypeTwo:
type: object
properties:
id:
type: string
format: uuid
date:
type: string
format: date-time
someStrings:
type: array
items:
type: string

TypeThree:
allOf:
- $ref: "#/components/schemas/SuperTypeTwo"
type: object
properties:
type:
type: string
enum: [TypeThree]
default: TypeThree
somethings:
type: array
minItems: 1
maxItems: 2
items:
oneOf:
- $ref: "#/components/schemas/TypeOne"
- $ref: "#/components/schemas/TypeTwo"
discriminator:
propertyName: type
required:
- type
- somethings