Skip to content
Merged
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
19 changes: 19 additions & 0 deletions packages/utils/json-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ describe('json-schema utility functions', () => {
})
})

it('should keep a property named $ref when making local definitions', async () => {
const schema = makeLocalDefs({
'http://test.com/schema1': {
type: 'object',
properties: {
ref: { $ref: 'http://test.com/schema2' }
}
},
'http://test.com/schema2': {
type: 'object',
properties: {
$ref: { type: 'string', format: 'uri-reference' }
}
}
}, 'http://test.com/schema1')
assert.equal(schema.properties.ref.$ref, '#/$defs/schema2')
assert.deepEqual(schema.$defs.schema2.properties.$ref, { type: 'string', format: 'uri-reference' })
})

it('should transform absolute references to local definitions', async () => {
const schema = makeLocalDefs({
'http://test.com/schema1': {
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ const recurseLocalDefs = (
) => {
if (!schemaFragment) throw new Error('failed to resolve ' + schemaId)
for (const [key, value] of Object.entries(schemaFragment)) {
if (key === '$ref') {
// a non-string $ref is a property named "$ref" (OpenAPI 3.1 meta-schema), not a reference
if (key === '$ref' && typeof value === 'string') {
if (schemaId !== originalSchemaId || !value.startsWith('#')) {
let fullId
try {
Expand All @@ -236,7 +237,7 @@ const recurseLocalDefs = (
throw new Error(`Failed to normalize URL ${value} from ${schemaId}`)
}
const [refSchemaId, refSchemaPath] = fullId.split('#')
const refName = value.split('/').pop()
const refName = value.slice(value.lastIndexOf('/') + 1)
if (!localDefsSchemas[refName]) {
let refSchema = schemas[refSchemaId]
if (!refSchema) throw new Error('failed to resolve schema ' + refSchemaId)
Expand Down
Loading