Skip to content

Add URI::Params::Field ignore options#17037

Open
RoySerbi wants to merge 4 commits into
crystal-lang:masterfrom
RoySerbi:add-uri-params-field-ignore-options
Open

Add URI::Params::Field ignore options#17037
RoySerbi wants to merge 4 commits into
crystal-lang:masterfrom
RoySerbi:add-uri-params-field-ignore-options

Conversation

@RoySerbi

@RoySerbi RoySerbi commented Jun 1, 2026

Copy link
Copy Markdown

Resolves #16873.

This adds the same basic field-skipping options to URI::Params::Field that are already available on JSON::Field and YAML::Field:

  • ignore: true skips both serialization and deserialization
  • ignore_serialize: true skips #to_www_form
  • ignore_deserialize: true skips .from_www_form

I also updated the URI::Params::Serializable docs and added specs covering each case.

Local check:

  • git diff --check

I could not run the Crystal specs locally because this Windows environment does not have the Crystal compiler installed.

Comment thread src/uri/params/serializable.cr Outdated
@straight-shoota straight-shoota modified the milestone: 1.21.0 Jun 1, 2026
@straight-shoota

Copy link
Copy Markdown
Member

There is already #17029 pending for the same feature and the implementation looks better: #16873 (comment)

RoySerbi added 2 commits June 1, 2026 19:30
Mirrors the JSON::Serializable convention: when ignore_serialize: is a
literal 'true', the field is skipped at compile time (existing behaviour).
When it's any other Crystal expression, the emit is wrapped in
'unless {{expr}}' so the expression is evaluated at runtime, allowing
conditional serialization based on instance state, e.g.

    @[URI::Params::Field(ignore_serialize: internal)]
    @token : String

See src/json/serialization.cr for the matching pattern in
JSON::Serializable. Updates docstring and adds a spec for the
runtime-expression case.
@RoySerbi

RoySerbi commented Jun 1, 2026

Copy link
Copy Markdown
Author

@straight-shoota Good catch on the parity with #17029 — looking at src/json/serialization.cr, JSON::Serializable's ignore_serialize accepts any Crystal expression and evaluates it at runtime (unless {{ value[:ignore_serialize] }}), which is exactly what #17029 mirrored and what this PR was missing.

7cd19a6 brings this PR to the same convention:

  • Literal ignore_serialize: true is still elided at compile time (no codegen change for the common case).
  • Any other expression is emitted as unless {{ expr }} around the field's to_www_form call, so ignore_serialize: internal (or any boolean expression over instance state) works at runtime.
  • Docstring updated to match JSON::Field's wording verbatim.
  • Added a spec exercising the runtime-expression case (truthy skips, falsy emits).

The deserialization side is unchanged (still compile-time only for ignore / ignore_deserialize), matching JSON::Serializable's asymmetry. Happy to fold in trypsynth's spec style or any other tweaks if you'd prefer this over #17029.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ignore and ignore_deserialize properties to URI::Params::Field

4 participants