Fix "create an item" requestBody schema in OAS (#16294)

This commit is contained in:
Azri Kahar
2022-11-10 11:50:01 +08:00
committed by GitHub
parent 10ebe7c523
commit cc55d5608e
2 changed files with 104 additions and 54 deletions

View File

@@ -32,69 +32,120 @@ describe('Integration Tests', () => {
service = new SpecificationService({
knex: db,
schema: { collections: {}, relations: [] },
accountability: { role: 'admin', admin: true },
});
});
it('returns untyped schema for json fields', async () => {
jest.spyOn(CollectionsService.prototype, 'readByQuery').mockImplementation(
jest.fn().mockReturnValue([
{
collection: 'test_table',
meta: {
accountability: 'all',
describe('schema', () => {
it('returns untyped schema for json fields', async () => {
jest.spyOn(CollectionsService.prototype, 'readByQuery').mockImplementation(
jest.fn().mockReturnValue([
{
collection: 'test_table',
group: null,
hidden: false,
icon: null,
item_duplication_fields: null,
note: null,
singleton: false,
translations: null,
meta: {
accountability: 'all',
collection: 'test_table',
group: null,
hidden: false,
icon: null,
item_duplication_fields: null,
note: null,
singleton: false,
translations: null,
},
schema: {
name: 'test_table',
},
},
schema: {
name: 'test_table',
},
},
])
);
])
);
jest.spyOn(FieldsService.prototype, 'readAll').mockImplementation(
jest.fn().mockReturnValue([
{
collection: 'test_table',
field: 'id',
type: 'integer',
schema: {
is_nullable: false,
},
},
{
collection: 'test_table',
field: 'blob',
type: 'json',
schema: {
is_nullable: true,
},
},
])
);
jest.spyOn(RelationsService.prototype, 'readAll').mockImplementation(jest.fn().mockReturnValue([]));
const spec = await service.oas.generate();
expect(spec.components?.schemas).toEqual({
ItemsTestTable: {
type: 'object',
properties: {
id: {
nullable: false,
jest.spyOn(FieldsService.prototype, 'readAll').mockImplementation(
jest.fn().mockReturnValue([
{
collection: 'test_table',
field: 'id',
type: 'integer',
schema: {
is_nullable: false,
},
},
blob: {
nullable: true,
{
collection: 'test_table',
field: 'blob',
type: 'json',
schema: {
is_nullable: true,
},
},
])
);
jest.spyOn(RelationsService.prototype, 'readAll').mockImplementation(jest.fn().mockReturnValue([]));
const spec = await service.oas.generate();
expect(spec.components?.schemas).toEqual({
ItemsTestTable: {
type: 'object',
properties: {
id: {
nullable: false,
type: 'integer',
},
blob: {
nullable: true,
},
},
'x-collection': 'test_table',
},
'x-collection': 'test_table',
},
});
});
});
describe('path', () => {
it('requestBody for CreateItems POST path should not have type in schema', async () => {
const collection = {
collection: 'test_table',
meta: {
accountability: 'all',
collection: 'test_table',
group: null,
hidden: false,
icon: null,
item_duplication_fields: null,
note: null,
singleton: false,
translations: null,
},
schema: {
name: 'test_table',
},
};
jest
.spyOn(CollectionsService.prototype, 'readByQuery')
.mockImplementation(jest.fn().mockReturnValue([collection]));
jest.spyOn(FieldsService.prototype, 'readAll').mockImplementation(
jest.fn().mockReturnValue([
{
collection: collection.collection,
field: 'id',
type: 'integer',
schema: {
is_nullable: false,
},
},
])
);
jest.spyOn(RelationsService.prototype, 'readAll').mockImplementation(jest.fn().mockReturnValue([]));
const spec = await service.oas.generate();
const targetSchema =
spec.paths[`/items/${collection.collection}`].post.requestBody.content['application/json'].schema;
expect(targetSchema).toHaveProperty('oneOf');
expect(targetSchema).not.toHaveProperty('type');
});
});
});

View File

@@ -44,7 +44,6 @@ post:
content:
application/json:
schema:
type: object
responses:
'200':
description: Successful request