Skip to content

fix: fix no-$ref-siblings error #1048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
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
45 changes: 32 additions & 13 deletions packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,33 @@ export function schemaToOpenAPI(
const { example, minItems, maxItems, ...rest } = defaultOpenAPIObject;
const isArrayExample = example && Array.isArray(example);

const siblings = {
...rest,
...(!isArrayExample && example ? { example } : {}),
};

// Handle case where innerSchema is a $ref with siblings
const wrappedInnerSchema =
'$ref' in innerSchema && Object.keys(siblings).length > 0
? // When there's a $ref with siblings, we need to wrap it in allOf to preserve other properties
{
allOf: [innerSchema],
}
: {
...innerSchema,
};

const items = {
...wrappedInnerSchema,
...siblings,
};

return {
type: 'array',
...(minItems ? { minItems } : {}),
...(maxItems ? { maxItems } : {}),
...(isArrayExample ? { example } : {}),
items: {
...innerSchema,
...rest,
...(!isArrayExample && example ? { example } : {}),
},
items,
};
case 'object':
return {
Expand Down Expand Up @@ -153,23 +170,25 @@ export function schemaToOpenAPI(
if (oneOf.length === 0) {
return undefined;
} else if (oneOf.length === 1) {
if (
Object.keys(
oneOf[0] as OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
)[0] === '$ref'
)
const singleSchema = oneOf[0];
if (singleSchema === undefined) {
return undefined;
}
// Check if the schema is a $ref
if ('$ref' in singleSchema) {
// OpenAPI spec doesn't allow $ref properties to have siblings, so they're wrapped in an 'allOf' array
return {
...(nullable ? { nullable } : {}),
allOf: oneOf,
allOf: [singleSchema],
...defaultOpenAPIObject,
};
else
} else {
return {
...(nullable ? { nullable } : {}),
...oneOf[0],
...singleSchema,
...defaultOpenAPIObject,
};
}
} else {
return { ...(nullable ? { nullable } : {}), oneOf, ...defaultOpenAPIObject };
}
Expand Down
200 changes: 199 additions & 1 deletion packages/openapi-generator/test/openapi/ref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ const SimpleRouteResponse = t.type({
* @title Human Readable Invalid Error Schema
*/
const InvalidError = t.intersection([
ApiError,
ApiError,
t.type({ error: t.literal('invalid') })]);
/**
* Human readable description of the ApiError schema
Expand Down Expand Up @@ -441,3 +441,201 @@ testCase('route with api error schema', ROUTE_WITH_SCHEMA_WITH_COMMENT, {
},
},
});

const ROUTE_WITH_ARRAY_OF_INNER_SCHEMA_REF_WITH_NO_SIBLINGS = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
/**
* A simple route with type descriptions for references
*
* @operationId api.v1.test
* @tag Test Routes
*/
export const route = h.httpRoute({
path: '/foo',
method: 'GET',
request: h.httpRequest({}),
response: {
200: SimpleRouteResponse
},
});


/**
* Human readable description of the Simple Route Response
* @title Human Readable Simple Route Response
*/
const SimpleRouteResponse = t.type({
test: t.array(TestRef)
});

const TestRefBase = t.string;

const TestRef = TestRefBase;
`;

testCase(
'inner schema ref in array',
ROUTE_WITH_ARRAY_OF_INNER_SCHEMA_REF_WITH_NO_SIBLINGS,
{
openapi: '3.0.3',
info: {
title: 'Test',
version: '1.0.0',
},
paths: {
'/foo': {
get: {
summary: 'A simple route with type descriptions for references',
operationId: 'api.v1.test',
tags: ['Test Routes'],
parameters: [],
responses: {
'200': {
description: 'OK',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/SimpleRouteResponse',
},
},
},
},
},
},
},
},
components: {
schemas: {
SimpleRouteResponse: {
description: 'Human readable description of the Simple Route Response',
properties: {
test: {
type: 'array',
items: {
$ref: '#/components/schemas/TestRefBase',
},
},
},
required: ['test'],
title: 'Human Readable Simple Route Response',
type: 'object',
},
TestRefBase: {
title: 'TestRefBase',
type: 'string',
},
TestRef: {
allOf: [
{
title: 'TestRef',
},
{
$ref: '#/components/schemas/TestRefBase',
},
],
},
},
},
},
);

const ROUTE_WITH_ARRAY_OF_INNER_SCHEMA_REF_AND_DESCRIPTION = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
/**
* A simple route with type descriptions for references
*
* @operationId api.v1.test
* @tag Test Routes
*/
export const route = h.httpRoute({
path: '/foo',
method: 'GET',
request: h.httpRequest({}),
response: {
200: SimpleRouteResponse
},
});


/**
* Human readable description of the Simple Route Response
* @title Human Readable Simple Route Response
*/
const SimpleRouteResponse = t.type({
/** List of test refs */
test: t.array(TestRef)
});

const TestRefBase = t.string;

const TestRef = TestRefBase;
`;

testCase(
'inner schema ref in array with description',
ROUTE_WITH_ARRAY_OF_INNER_SCHEMA_REF_AND_DESCRIPTION,
{
openapi: '3.0.3',
info: {
title: 'Test',
version: '1.0.0',
},
paths: {
'/foo': {
get: {
summary: 'A simple route with type descriptions for references',
operationId: 'api.v1.test',
tags: ['Test Routes'],
parameters: [],
responses: {
'200': {
description: 'OK',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/SimpleRouteResponse',
},
},
},
},
},
},
},
},
components: {
schemas: {
SimpleRouteResponse: {
description: 'Human readable description of the Simple Route Response',
properties: {
test: {
type: 'array',
items: {
allOf: [{ $ref: '#/components/schemas/TestRefBase' }],
description: 'List of test refs',
},
},
},
required: ['test'],
title: 'Human Readable Simple Route Response',
type: 'object',
},
TestRefBase: {
title: 'TestRefBase',
type: 'string',
},
TestRef: {
allOf: [
{
title: 'TestRef',
},
{
$ref: '#/components/schemas/TestRefBase',
},
],
},
},
},
},
);
Loading