Skip to content

[OpenAPI]: Add support for blueprinter root key#343

Merged
rsamoilov merged 6 commits into
rage-rb:mainfrom
Abishekcs:openapi/blueprinter-root-key
Jul 8, 2026
Merged

[OpenAPI]: Add support for blueprinter root key#343
rsamoilov merged 6 commits into
rage-rb:mainfrom
Abishekcs:openapi/blueprinter-root-key

Conversation

@Abishekcs

@Abishekcs Abishekcs commented Jul 1, 2026

Copy link
Copy Markdown
Member

What this PR does?

Add Support for root key

Example

  # @response DataMining(view: :extended, root: :user)

Screnshot

  • Before
image
  • After
image

Abishekcs added 4 commits July 1, 2026 16:58
…ations

Add support for the `root:` option in Blueprinter parser, wrapping the schema under a named key when specified (e.g. `@response UserBlueprint(root: :user)`).
@Abishekcs
Abishekcs force-pushed the openapi/blueprinter-root-key branch from b5e1f27 to a8f4eda Compare July 1, 2026 11:28
@Abishekcs Abishekcs changed the title Add support for blueprinter root key [OpenAPI]: Add support for blueprinter root key Jul 1, 2026
@Abishekcs

Copy link
Copy Markdown
Member Author

PR is ready for review!

end
end

describe "collection" do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can see parsing fails with collections and string-based root keys, e.g. Array<UserBlueprint(root: 'user')> and Array<UserBlueprint(root: "user")>.

RUBY
end

it "wraps the array items schema under the specified root key" do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The wording seems correct, but your test looks different. The test expects a structure like [{ root_key: { ... } }], but the correct structure seems to be { root_key: [{ ... }] }.

@Abishekcs Abishekcs Jul 1, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I thought the root key gets wrapped in array too. Didn't check the actual blueprinter output for collection, should had done that earlier😐

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed it

image

…t: key wrapping

- Add single and double quote support to `__try_parse_collection` regex so string values (e.g. `root: 'users'`) parse correctly in collection annotations.
- Symbolize view name before reflections lookup so `view: 'normal'` and`view: :normal` both resolve correctly.
- Fix root: key wrapping to apply after is_collection, so the root key wraps the full array schema rather than individual items.
@parsing_stack.add(klass.name)

view_name = serializer_options&.key?(:view) ? serializer_options[:view] : :default
view_name = serializer_options&.key?(:view) ? serializer_options[:view].to_sym : :default

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Convert to Symbol to_sym as reflection api errors out with string.

result = { "type" => "array", "items" => result } if is_collection
result

if serializer_options&.key?(:root)

@Abishekcs Abishekcs Jul 2, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

the root key now wraps the fully-resolved schema (including the array wrapper if it's a collection), rather than wrapping just the inner properties before the array wrapper is applied.

# @private
def self.__try_parse_collection(str)
if str =~ /^Array<([\w\s:\(\),]+)>$/ || str =~ /^\[([\w\s:\(\),]+)\]$/
if str =~ /^Array<([\w\s:\(\),'\"]+)>$/ || str =~ /^\[([\w\s:\(\),'\"]+)\]$/

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It was due to this String "" or '' was failing as it didn't support it.

@Abishekcs

Copy link
Copy Markdown
Member Author

@rsamoilov I have made the required changes. And also added some new test cases to test different combination of String or Symbol with view and root.

@rsamoilov rsamoilov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🔥

@rsamoilov
rsamoilov merged commit 0204c9a into rage-rb:main Jul 8, 2026
11 checks passed
@Abishekcs
Abishekcs deleted the openapi/blueprinter-root-key branch July 9, 2026 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants