Files
gitbook/packages/react-openapi/src/OpenAPISchema.test.ts
T
2025-04-16 09:14:12 +00:00

173 lines
4.6 KiB
TypeScript

import { describe, expect, it } from 'bun:test';
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
import { getSchemaAlternatives } from './OpenAPISchema';
describe('getSchemaAlternatives', () => {
it('should flatten oneOf', () => {
expect(
getSchemaAlternatives({
oneOf: [
{
oneOf: [
{
type: 'number',
},
{
type: 'boolean',
},
],
},
{
type: 'string',
},
],
})
).toEqual([
{
type: 'number',
},
{
type: 'boolean',
},
{
type: 'string',
},
]);
});
it('merges string enum', () => {
expect(
getSchemaAlternatives({
oneOf: [
{
oneOf: [
{
type: 'string',
enum: ['a', 'b'],
},
{
type: 'string',
enum: ['c', 'd'],
nullable: true,
},
],
},
],
})
).toEqual([
{
type: 'string',
enum: ['a', 'b', 'c', 'd'],
nullable: true,
},
]);
});
it('merges objects with allOf', () => {
expect(
getSchemaAlternatives({
allOf: [
{
type: 'object',
properties: {
name: {
type: 'string',
},
map: {
type: 'string',
},
description: {
type: 'string',
},
},
required: ['name'],
},
{
type: 'object',
properties: {
externalId: {
type: 'string',
},
},
required: ['map', 'externalId'],
},
],
})
).toEqual([
{
type: 'object',
properties: {
name: {
type: 'string',
},
map: {
type: 'string',
},
description: {
type: 'string',
},
externalId: {
type: 'string',
},
},
required: ['name', 'map', 'externalId'],
},
]);
});
it('should not flatten oneOf and allOf', () => {
expect(
getSchemaAlternatives({
oneOf: [
{
allOf: [
{
type: 'number',
},
{
type: 'boolean',
},
],
},
{
type: 'string',
},
],
})
).toEqual([
{
allOf: [
{
type: 'number',
},
{
type: 'boolean',
},
],
},
{
type: 'string',
},
]);
});
it('should stop at circular references', () => {
const a: OpenAPIV3.SchemaObject = {
anyOf: [
{
type: 'string',
},
],
};
a.anyOf?.push(a);
expect(getSchemaAlternatives(a)).toEqual([
{
type: 'string',
},
a,
]);
});
});