Skip to content

Commit

Permalink
add oneOf test
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Oct 26, 2020
1 parent a832015 commit 4ad6017
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 27 deletions.
30 changes: 26 additions & 4 deletions test/oneof.readonly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as path from 'path';
import * as express from 'express';
import * as request from 'supertest';
import { createApp } from './common/app';
import { expect } from 'chai';

describe('one.of readonly', () => {
describe.only('one.of readonly', () => {
let app = null;

before(async () => {
Expand All @@ -13,7 +14,10 @@ describe('one.of readonly', () => {
app.use(
express
.Router()
.post(`${app.basePath}/orders`, (req, res) =>
.post(`${app.basePath}/any_of`, (req, res) =>
res.status(200).json({ success: true }),
)
.post(`${app.basePath}/one_of`, (req, res) =>
res.status(200).json({ success: true }),
),
),
Expand All @@ -24,10 +28,28 @@ describe('one.of readonly', () => {
app.server.close();
});

it('post type (without readonly id) should pass', async () =>
it('post type anyOf (without readonly id) should pass', async () =>
request(app)
.post(`${app.basePath}/orders`)
.post(`${app.basePath}/any_of`)
.send({ type: 'A' })
.set('Content-Type', 'application/json')
.expect(200));

it('post type oneOf (without readonly id) should pass', async () =>
request(app)
.post(`${app.basePath}/one_of`)
.send({ type: 'A' })
.set('Content-Type', 'application/json')
.expect(400)
.then((r) => {
const error = r.body;
expect(error.message).to.include('to one of the allowed values: C, D');
}));

it('post type oneOf (without readonly id) should pass', async () =>
request(app)
.post(`${app.basePath}/one_of`)
.send({ type: 'C' })
.set('Content-Type', 'application/json')
.expect(200));
});
70 changes: 47 additions & 23 deletions test/oneof.readonly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ info:
servers:
- url: /v1
paths:
'/orders':
/any_of:
post:
requestBody:
content:
Expand All @@ -24,6 +24,24 @@ paths:
200:
description: successful operation

/one_of:
post:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/subC'
- $ref: '#/components/schemas/subD'
discriminator:
propertyName: type
mapping:
A: '#/components/schemas/subC'
B: '#/components/schemas/subD'
responses:
200:
description: successful operation

components:
schemas:
common:
Expand All @@ -42,29 +60,35 @@ components:
subA:
allOf:
- $ref: '#/components/schemas/common'
# - type: object
# properties:
# id:
# readOnly: true
# type: string
# type:
# type: string
# enum: [A, B]
# required:
# - id
# - type

subB:
allOf:
- $ref: '#/components/schemas/common'
# - type: object
# properties:
# id:
# readOnly: true
# type: string
# type:
# type: string
# enum: [A, B]
# required:
# - id
# - type

subC:
allOf:
- type: object
properties:
id:
readOnly: true
type: string
type:
type: string
enum: [C, D]
required:
- id
- type

subD:
allOf:
- type: object
properties:
id:
readOnly: true
type: string
type:
type: string
enum: [C, D]
required:
- id
- type

0 comments on commit 4ad6017

Please sign in to comment.