Skip to content

[OpenAPI]: Parse basic Blueprinter fields: identifier, field, and fields#300

Open
Abishekcs wants to merge 3 commits into
rage-rb:mainfrom
Abishekcs:openapi/blueprinter-basic-fields
Open

[OpenAPI]: Parse basic Blueprinter fields: identifier, field, and fields#300
Abishekcs wants to merge 3 commits into
rage-rb:mainfrom
Abishekcs:openapi/blueprinter-basic-fields

Conversation

@Abishekcs
Copy link
Copy Markdown
Contributor

@Abishekcs Abishekcs commented May 19, 2026

closes #289

What this PR does?

Adds support for parsing basic Blueprinter fields:

  • identifier
  • field
  • fields

Also supports:

  • Field renaming via the name: option (e.g. field :email, name: :login)
  • Block fields (e.g. field(:full_name) { |u| ... }) , defaults to string type since the block can't be statically evaluated
  • Collections via Array<UserBlueprint> or [UserBlueprint] syntax

Sample Code

  • routes.rb
Rage.routes.draw do
  root to: ->(env) { [200, {}, ["It works!"]] }

  get "/users/:id/blue", to: "users#show_blue"
end
  • products_controller.rb
class UsersController < RageController::API
  
  # @response UserBlueprint
  def show_blue
    user = [{ uuid: 1, id: 101, name: "Alice", email: "alice@example.com", age: 25, first_name: "Alice", last_name: "loom", abishek: 'abi' }]
    render json: UserBlueprint.render(user)
  end
end
  • user_blueprint.rb
class UserBlueprint < Blueprinter::Base
  identifier :uuid
  
  fields :id, :name, :email, :age
  
  field :email, name: :login   # -> "login": string

  field :abishek, name: :ad   # -> "login": string
  
  fields :first_name, :last_name

  field(:full_name) { |u| "#{u[:first_name]} #{u[:last_name]}" }
end

Screenrecording

  • Before
image
Before.mp4
  • After
image
After.mp4

collection

image
After.Collection.mp4

@Abishekcs Abishekcs force-pushed the openapi/blueprinter-basic-fields branch from b4b6c8b to ff9d969 Compare May 19, 2026 09:23
- Statically parses top-level identifier, field, and fields
declarations.

- Maps each to type: string by default.

- Handles the `name`: option for key renaming

- Fields defined with a block also produce a string entry.

- Uses Prism AST visitor to statically analyze blueprint source files

- Supports Collections using Array<> or [] syntax for arrays

- Further Blueprinter features (views, association, transformer) will be
built on top of this foundation
@Abishekcs Abishekcs force-pushed the openapi/blueprinter-basic-fields branch from ff9d969 to da9b3a4 Compare May 26, 2026 08:32
@Abishekcs Abishekcs changed the title [WIP]: [OpenAPI]: Parse basic Blueprinter fields: identifier, field, and fields [OpenAPI]: Parse basic Blueprinter fields: identifier, field, and fields May 26, 2026
@Abishekcs Abishekcs marked this pull request as ready for review May 26, 2026 09:00
- Remove unused `@hashes` from VisitorContext
- Use `node.value.unescaped` instead of `node.value.slice` in
  `visit_assoc_node` for cleaner string extraction
- Replace `context.keywords.any?` with `context.keywords["name"]`
  for a more explicit nil check
- Remove `delete_prefix(":")` since `unescaped` returns the clean
  value without the colon prefix
@Abishekcs
Copy link
Copy Markdown
Contributor Author

Hi @rsamoilov can you review this PR whenever you're free. Thank you!

@rsamoilov
Copy link
Copy Markdown
Member

Hey @Abishekcs ,

I'm so sorry about the delay! I'll be looking closely into the changes this weekend.

@Abishekcs
Copy link
Copy Markdown
Contributor Author

No worries. Take your time.

Copy link
Copy Markdown
Member

@rsamoilov rsamoilov left a comment

Choose a reason for hiding this comment

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

Hi @Abishekcs ,

This looks great, and I like the tests too!

Several observations:

  1. the parser currently only supports symbol field names, although Blueprinter works fine with string fields too:
class UserBlueprint < Blueprinter::Base
  fields "id", "name", "email", "age"
end
  1. One of the guarantees Blueprinter provides for the identifier fields is that "When rendering, identifier fields are always sorted first, before other fields". Let's also ensure identifiers are rendered first in the OpenAPI spec.

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.

Parse basic fields: identifier, field, fields

2 participants