Skip to content

Fix merge logic for allOf alternatives #3515

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .changeset/tame-months-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': patch
---

Fix merge logic for allOf alternatives
18 changes: 11 additions & 7 deletions packages/react-openapi/src/OpenAPISchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,10 @@ function mergeAlternatives(
schemaOrRef.enum
) {
const keys = Object.keys(schemaOrRef);
if (keys.every((key) => ['type', 'enum', 'nullable'].includes(key))) {
latest.enum = Array.from(new Set([...latest.enum, ...schemaOrRef.enum]));
if (keys.some((key) => ['type', 'enum', 'nullable'].includes(key))) {
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing from every() to some() fundamentally alters the merge condition logic. This will now merge schemas if ANY of the keys match the allowed list, rather than requiring ALL keys to match. This could cause unintended merging of schemas that have additional properties beyond the allowed set.

Suggested change
if (keys.some((key) => ['type', 'enum', 'nullable'].includes(key))) {
if (keys.every((key) => ['type', 'enum', 'nullable'].includes(key))) {

Copilot uses AI. Check for mistakes.

latest.enum = Array.from(
new Set([...(latest.enum || []), ...(schemaOrRef.enum || [])])
);
latest.nullable = latest.nullable || schemaOrRef.nullable;
return acc;
}
Expand All @@ -566,18 +568,20 @@ function mergeAlternatives(
if (latest && latest.type === 'object' && schemaOrRef.type === 'object') {
const keys = Object.keys(schemaOrRef);
if (
keys.every((key) =>
keys.some((key) =>
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the enum case above, changing from every() to some() will allow merging of object schemas even when they contain properties outside the allowed list ['type', 'properties', 'required', 'nullable']. This could lead to incorrect schema merging behavior.

Suggested change
keys.some((key) =>
keys.every((key) =>

Copilot uses AI. Check for mistakes.

['type', 'properties', 'required', 'nullable'].includes(key)
)
) {
latest.properties = {
...latest.properties,
...schemaOrRef.properties,
...(latest.properties || {}),
...(schemaOrRef.properties || {}),
};
latest.required = Array.from(
new Set([
...(Array.isArray(latest.required) ? latest.required : []),
...(Array.isArray(schemaOrRef.required)
...(latest.required && Array.isArray(latest.required)
? latest.required
: []),
...(schemaOrRef.required && Array.isArray(schemaOrRef.required)
? schemaOrRef.required
: []),
])
Expand Down
Loading