fix: do not expand empty list or associative array under key operators#85
Open
spokodev wants to merge 1 commit into
Open
fix: do not expand empty list or associative array under key operators#85spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
RFC 6570 section 2.3 defines a list or associative array with zero members
as undefined, so an undefined value contributes nothing to an expansion.
The key operators (?, &, ;) emitted a "name=" pair for an empty collection
anyway:
parseTemplate('{?list}').expand({ list: [] }); // was "?list=", now ""
The non-key branch already guarded on `tmp.length`; the key branch did not.
Guard both. An empty string value stays defined and still expands to
"name=", matching RFC 6570 section 3.2.8.
The official RFC 6570 test suite's "Empty Variables" cases now pass; two
unit-test assertions that encoded the old behavior are corrected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
An empty list or empty associative array expanded under a key operator (
?,&,;) emits a spuriousname=pair:RFC 6570 §2.3: "A variable defined as a list value is considered undefined if the list contains zero members" (likewise an associative array with zero members). An undefined value contributes nothing to an expansion, so the result must be the empty string.
An empty string is different — it is defined — and must still expand to
name=per §3.2.8. That case is unaffected.Cause
In
getValues, the non-key branch already guarded withtmp.length !== 0, but the key-operator branch pushedkey=unconditionally. For a zero-member collectiontmpis empty, so the guard belongs on both branches.Fix
Verification
extended-tests.json) now pass;spec-examples-by-section.jsonstays at 117/117 andspec-examples.jsonat 64/64 — zero regression.test/url-template-test.jsthat encoded the old behavior ({?emptylist}→?emptylist=,{?emptyobject}→?emptyobject=) are corrected to'', alongside new&/;cases. Note these contradicted the same file's own{?emptylist*}→''assertion.