[PR #5990] fix(import): handle allOf in OpenAPI example body generation #5455

Open
opened 2026-03-17 02:53:40 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hoppscotch/hoppscotch/pull/5990
Author: @mahmoodhamdi
Created: 3/13/2026
Status: 🔄 Open

Base: mainHead: fix/openapi-allof-example-generation


📝 Commits (3)

  • 1d2d339 fix(import): handle allOf in OpenAPI example body generation
  • fefb734 fix: preserve merged object when allOf sub-schema has no type
  • e77e812 fix: guard oneOf/anyOf against empty arrays

📊 Changes

2 files changed (+74 additions, -5 deletions)

View changed files

📝 packages/hoppscotch-common/src/helpers/import-export/import/openapi/example-generators/v3.ts (+34 -4)
📝 packages/hoppscotch-common/src/helpers/import-export/import/openapi/example-generators/v31.ts (+40 -1)

📄 Description

Description

Closes #5988

When importing OpenAPI specs that use allOf for schema composition (a very common pattern for inheritance and mixins), the example body generator produced an empty string because allOf was not handled.

For example, this schema:

allOf:
  - $ref: '#/components/schemas/BaseAnimal'
  - type: object
    properties:
      breed:
        type: string

Previously generated: "" (empty)
Now generates: {"name": "string", "age": 0, "breed": "string"}

Changes

v3.ts (OpenAPI 3.0):

  • Added allOf handling that merges all sub-schema properties into a single object
  • Replaced the TODO comment with working implementation

v31.ts (OpenAPI 3.1):

  • Added the same allOf handling
  • Also added the missing oneOf/anyOf handling (had a TODO noting the gap)

The merge strategy: iterate sub-schemas left to right, generate examples for each, and spread-merge object results. Later schemas override earlier ones for conflicting keys, consistent with how allOf works in JSON Schema.

Testing

  • All 680 existing tests in hoppscotch-common pass
  • Full typecheck passes across all packages
  • Lint passes (0 errors, only pre-existing warnings)

Summary by cubic

Fixes empty example bodies when importing OpenAPI specs that use allOf by merging composed schemas and preserving merged objects when a sub-schema has no type. Also adds default handling for oneOf/anyOf in 3.0/3.1 to pick the first option and guards against empty arrays.

  • Bug Fixes
    • OpenAPI 3.0/3.1: merge allOf sub-schemas left-to-right; later keys override earlier; skip non-object results so constraint-only sub-schemas don’t wipe merged objects.
    • OpenAPI 3.0/3.1: handle oneOf/anyOf by generating from the first schema; guard against empty arrays to prevent runtime errors.
    • Example bodies now include inherited/mixed-in properties instead of an empty string.

Written for commit e77e8123e7. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hoppscotch/hoppscotch/pull/5990 **Author:** [@mahmoodhamdi](https://github.com/mahmoodhamdi) **Created:** 3/13/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/openapi-allof-example-generation` --- ### 📝 Commits (3) - [`1d2d339`](https://github.com/hoppscotch/hoppscotch/commit/1d2d3394aa9d98e375f00f54df9036bf4e66f63b) fix(import): handle allOf in OpenAPI example body generation - [`fefb734`](https://github.com/hoppscotch/hoppscotch/commit/fefb734891fdf7fe3d1cb3036fabf3255d172eb5) fix: preserve merged object when allOf sub-schema has no type - [`e77e812`](https://github.com/hoppscotch/hoppscotch/commit/e77e8123e72afef344cf329ca91cf9b1926f21d5) fix: guard oneOf/anyOf against empty arrays ### 📊 Changes **2 files changed** (+74 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/hoppscotch-common/src/helpers/import-export/import/openapi/example-generators/v3.ts` (+34 -4) 📝 `packages/hoppscotch-common/src/helpers/import-export/import/openapi/example-generators/v31.ts` (+40 -1) </details> ### 📄 Description ### Description Closes #5988 When importing OpenAPI specs that use `allOf` for schema composition (a very common pattern for inheritance and mixins), the example body generator produced an empty string because `allOf` was not handled. For example, this schema: ```yaml allOf: - $ref: '#/components/schemas/BaseAnimal' - type: object properties: breed: type: string ``` Previously generated: `""` (empty) Now generates: `{"name": "string", "age": 0, "breed": "string"}` ### Changes **v3.ts (OpenAPI 3.0):** - Added `allOf` handling that merges all sub-schema properties into a single object - Replaced the TODO comment with working implementation **v31.ts (OpenAPI 3.1):** - Added the same `allOf` handling - Also added the missing `oneOf`/`anyOf` handling (had a TODO noting the gap) The merge strategy: iterate sub-schemas left to right, generate examples for each, and spread-merge object results. Later schemas override earlier ones for conflicting keys, consistent with how `allOf` works in JSON Schema. ### Testing - All 680 existing tests in hoppscotch-common pass - Full typecheck passes across all packages - Lint passes (0 errors, only pre-existing warnings) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes empty example bodies when importing OpenAPI specs that use `allOf` by merging composed schemas and preserving merged objects when a sub-schema has no type. Also adds default handling for `oneOf`/`anyOf` in 3.0/3.1 to pick the first option and guards against empty arrays. - **Bug Fixes** - OpenAPI 3.0/3.1: merge `allOf` sub-schemas left-to-right; later keys override earlier; skip non-object results so constraint-only sub-schemas don’t wipe merged objects. - OpenAPI 3.0/3.1: handle `oneOf`/`anyOf` by generating from the first schema; guard against empty arrays to prevent runtime errors. - Example bodies now include inherited/mixed-in properties instead of an empty string. <sup>Written for commit e77e8123e72afef344cf329ca91cf9b1926f21d5. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hoppscotch#5455
No description provided.