Skip to content

fix(core/json): stably_encode fails to encode cjson.null values#13281

Open
rickyxucn wants to merge 1 commit intoapache:masterfrom
rickyxucn:fix-stably-encode-cjson-null
Open

fix(core/json): stably_encode fails to encode cjson.null values#13281
rickyxucn wants to merge 1 commit intoapache:masterfrom
rickyxucn:fix-stably-encode-cjson-null

Conversation

@rickyxucn
Copy link
Copy Markdown

@rickyxucn rickyxucn commented Apr 22, 2026

The stably_encode function used dkjson.encode directly, which does not recognize cjson.null (a userdata) and fails to encode it properly.

This fix adds a convert_cjson_null helper that recursively converts cjson.null to dkjson.null before encoding. This ensures tables containing cjson.null values can be stably encoded as valid JSON with null literals.

Also added 4 test cases in t/core/json.t to cover:

  • simple table with cjson.null
  • nested table with cjson.null
  • encoding stability with cjson.null
  • top-level cjson.null encoding

Description

Which issue(s) this PR fixes:

Fixes #

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Apr 22, 2026
The stably_encode function used dkjson.encode directly, which does not
recognize cjson.null (a userdata) and fails to encode it properly.

This fix adds a convert_cjson_null helper that recursively converts
cjson.null to dkjson.null before encoding. This ensures tables containing
cjson.null values can be stably encoded as valid JSON with null literals.

Also added 4 test cases in t/core/json.t to cover:
- simple table with cjson.null
- nested table with cjson.null
- encoding stability with cjson.null
- top-level cjson.null encoding

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@rickyxucn rickyxucn force-pushed the fix-stably-encode-cjson-null branch from 1ecd73b to 2dbb890 Compare April 23, 2026 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant