Skip to content
Open
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
56 changes: 56 additions & 0 deletions src/open-api/utils/dereference.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,59 @@ it('dereferences', async () => {
}
`)
})

it('merges schemas defined using allOf', async () => {
await expect(
dereference({
foo: {
allOf: [
{
$ref: '#/components/schemas/User',
},
{
type: 'object',
properties: {
street: { type: 'string' },
},
},
],
},
components: {
schemas: {
User: {
type: 'object',
properties: {
name: { type: 'string' },
},
},
},
},
}),
).resolves.toMatchInlineSnapshot(`
{
"components": {
"schemas": {
"User": {
"properties": {
"name": {
"type": "string",
},
},
"type": "object",
},
},
},
"foo": {
"properties": {
"name": {
"type": "string",
},
"street": {
"type": "string",
},
},
"type": "object",
},
}
`)
})
10 changes: 10 additions & 0 deletions src/open-api/utils/dereference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pointerToPath } from '@stoplight/json'
import { mergeSchemas } from './merge-schemas.js'

/**
* TODO: Support remote references.
Expand Down Expand Up @@ -33,6 +34,15 @@ export async function dereference(document: unknown, root?: any): Promise<any> {
}),
)

if ('allOf' in document && Array.isArray(document['allOf'])) {
const { allOf, ...siblings } = document
const merged = allOf.reduce(mergeSchemas, {})
for (const key of Object.keys(document)) {
Reflect.deleteProperty(document, key)
}
Object.assign(document, merged, siblings)
}

return document
}

Expand Down
43 changes: 43 additions & 0 deletions src/open-api/utils/merge-schemas.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { mergeSchemas } from './merge-schemas.js'

it('merges flat objects', () => {
expect(mergeSchemas({ a: 1 }, { b: 2 })).toEqual({ a: 1, b: 2 })
})

it('overrides primitive values with the source', () => {
expect(mergeSchemas({ a: 1 }, { a: 2 })).toEqual({ a: 2 })
})

it('deeply merges nested objects', () => {
expect(
mergeSchemas(
{ properties: { name: { type: 'string' } } },
{ properties: { age: { type: 'integer' } } },
),
).toEqual({
properties: {
name: { type: 'string' },
age: { type: 'integer' },
},
})
})

it('concatenates arrays', () => {
expect(mergeSchemas({ items: [1, 2] }, { items: [3, 4] })).toEqual({
items: [1, 2, 3, 4],
})
})

it('deduplicates primitive arrays', () => {
expect(
mergeSchemas({ required: ['id', 'name'] }, { required: ['name', 'email'] }),
).toEqual({ required: ['id', 'name', 'email'] })
})

it('returns source when target is not an object', () => {
expect(mergeSchemas('string', { a: 1 })).toEqual({ a: 1 })
})

it('returns source when source is not an object', () => {
expect(mergeSchemas({ a: 1 }, 'string')).toEqual('string')
})
26 changes: 26 additions & 0 deletions src/open-api/utils/merge-schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}

export function mergeSchemas(target: unknown, source: unknown): unknown {
if (!isPlainObject(target) || !isPlainObject(source)) {
return source
}

const result: Record<string, unknown> = { ...target }

for (const key of Object.keys(source)) {
const targetVal = result[key]
const sourceVal = source[key]

if (Array.isArray(targetVal) && Array.isArray(sourceVal)) {
result[key] = [...new Set([...targetVal, ...sourceVal])]
} else if (isPlainObject(targetVal) && isPlainObject(sourceVal)) {
result[key] = mergeSchemas(targetVal, sourceVal)
} else {
result[key] = sourceVal
}
}

return result
}
48 changes: 48 additions & 0 deletions test/oas/fixtures/response-all-of.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"openapi": "3.0.0",
"info": {
"title": "allOf specification",
"version": "1.0.0"
},
"basePath": "https://example.com",
"paths": {
"/user": {
"get": {
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/UserBase"
},
{
"example": {
"street": "123 Main St",
"town": "Springfield",
"country": "USA"
}
}
]
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"UserBase": {
"example": {
"id": "abc-123",
"firstName": "John",
"lastName": "Maverick"
}
}
}
}
}
26 changes: 26 additions & 0 deletions test/oas/oas-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,29 @@ it('supports a referenced response example', async () => {
},
])
})

it('supports a response example using allOf', async () => {
const document = require('./fixtures/response-all-of.json')
const handlers = await fromOpenApi(document)
expect(await inspectHandlers(handlers)).toEqual<InspectedHandler[]>([
{
handler: {
method: 'GET',
path: 'https://example.com/user',
},
response: {
status: 200,
statusText: 'OK',
headers: expect.arrayContaining([['content-type', 'application/json']]),
body: JSON.stringify({
id: 'abc-123',
firstName: 'John',
lastName: 'Maverick',
street: '123 Main St',
town: 'Springfield',
country: 'USA',
}),
},
},
])
})