Skip to content

ppx/test: cover encode behavior for @drop_default variants - #84

Merged
andreypopp merged 2 commits into
melange-community:mainfrom
Khady:louis/test-drop-default-gaps
Jun 10, 2026
Merged

ppx/test: cover encode behavior for @drop_default variants#84
andreypopp merged 2 commits into
melange-community:mainfrom
Khady:louis/test-drop-default-gaps

Conversation

@Khady

@Khady Khady commented Apr 28, 2026

Copy link
Copy Markdown
Collaborator

Existing e2e runtime tests only exercised the [@option] + [@json.drop_default] combination via drop_default_option. The other three drop-default shapes had only PPX-generation coverage in ppx_deriving_json_js.t / ppx_deriving_json_native.t, but no runtime assertion that the encoder actually drops the field.

This PR adds runtime cases for the three remaining shapes so the encode behavior (drop when value matches default vs. include otherwise) is visible in the cram output:

  • [@json.default E] [@json.drop_default] (flag form, non-option default; uses equal_<type> in scope)
  • [@json.default E] [@json.drop_default expr] (custom comparator form)
  • [@json.default E] [@json.drop_default_if_json_equal]

Each new type contributes two cases: value matching the default (field dropped) and value differing (field included). Behavior is identical between native and JS backends.

This PR was motivated by a bug in ppx_deriving_jsonschema, which was struggling to handle an absent value marked with [@option] [@drop_default], as such values are serialized to undefined by melange json. It seems like a useful information to capture in the tests. Unfortunately it is not very explicit.

Khady and others added 2 commits April 28, 2026 08:34
Existing e2e tests only exercised the [@option] + [@json.drop_default]
combination. Add runtime coverage for the three remaining shapes so the
encode behavior (drop when value matches default vs include otherwise)
is visible in the cram output:

- [@json.default E] [@json.drop_default] (flag form, non-option default)
- [@json.default E] [@json.drop_default expr] (custom comparator)
- [@json.default E] [@json.drop_default_if_json_equal]

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds an outer record whose default for a record-typed field is a let-bound
value, where the inner record's own field uses [@option] [@json.drop_default].
Exercises three cases:

- inner foo=None present: round-trips via {"inner":{}} (foo dropped)
- inner foo=Some 5 present: round-trips via {"inner":{"foo":5}}
- outer field missing: default kicks in and decodes to the empty inner record

Previously [@json.default] was only tested with scalar literal payloads.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Khady

Khady commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator Author

Worth merging?

@andreypopp
andreypopp merged commit ba2a9ee into melange-community:main Jun 10, 2026
1 check passed
jchavarri added a commit that referenced this pull request Jun 29, 2026
* main: (40 commits)
  Respect nonrec type declarations in json derivers (#94)
  ppx/test: cover encode behavior for @drop_default variants (#84)
  allow @@compact_variants to parse ["Tag"] in addition to "Tag" for nullary variants (#93)
  Make extra JSON fields allowed by default (#92)
  Fix error messages for `[@@json.compact_variants]` (#91)
  Revert "Fix error message for @@compact_variants" (#90)
  Fix error message for @@compact_variants (#87)
  ppx: add [@json.catch_all] for forward-compatible string enums (#88)
  classify: filter undefined values from JS objects (#86)
  Add primitives semantics table to readme (#82)
  Support @json.compact_variants on polyvariant types (#79)
  ppx: allow dropping arbitrary values with `[@drop_default]` (#77)
  .gitignore: .claude
  upgrade ppxlib to 0.36 / handle Ptyp_open (#60)
  build with Melange 6 / OCaml 5.4 (#72)
  fmt: format using OCamlformat 0.28 (#75)
  fix: use `opam-check-npm-deps` compatible with OPAM 2.5 (#74)
  native: re_export yojson
  browser: remove dup of_json_error_to_string
  native: +of_json_error_to_string
  ...
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