Skip to content

core: reject invalid enum members in collection get and subset - #15104

Open
santhiprakash wants to merge 1 commit into
dagger:mainfrom
santhiprakash:fix-collection-enum-keys
Open

santhiprakash wants to merge 1 commit into
dagger:mainfrom
santhiprakash:fix-collection-enum-keys

Conversation

@santhiprakash

Copy link
Copy Markdown

Fixes #15101

Problem

On a collection keyed by an enum, get(key:) and subset(keys:) accept any
enum name, whether or not it is a member. A name that isn't a member reaches
the key lookup, and the collection reports it as missing:

collection "Test" does not contain key "Red" in the current subset

Passed to an ordinary argument of the same enum, the same name is refused
correctly:

convert arg "color": invalid enum member "Red" for TestColor

Root cause

The key arguments take the bare enum input type from TypeDef.ToInput()
(dagql.EnumValueName), whose DecodeInput accepts any string without
checking the members. collectionKeyFromInput then keeps the caller's text
verbatim, so a non-member falls through to the text comparison and produces
the misleading "does not contain key" error. Stored keys already go through
the enum type (collectionKeys → ModuleEnumType.ConvertFromSDKResult),
which is the asymmetry that lets the misspelt name slip through.

Fix

Convert the requested keys through the collection's enum type before the
text comparison, for both get and subset. A new
collectionRequestedKeys helper resolves the enum decoder once per call
and checks each requested name with ModuleEnum.Lookup, so a non-member is
refused with the usual invalid enum member "Red" for TestColor error that
an ordinary argument produces. The check uses the member names the schema
exposes (Local = false), matching how callers spell keys, while stored
keys keep being decoded by their module-original names.

No change to the stored-key path, the valid-key text form, subset
ordering, or the "does not contain key" error for a valid member that is
simply absent from the current subset.

Verification

New unit test TestCollectionEnumRequestedKeys in
core/collection_test.go builds an enum-keyed collection (RED/BLUE
members stored under their original names ColorRed/ColorBlue) and
covers: stored keys decoding to canonical member names, a valid member
selecting normally, a non-member (Red) refused with
invalid enum member "Red" for TestColor, a member absent from the current
subset still reported missing, and non-enum keys converted without a member
check.

$ go test ./core/ -run 'TestCollection' -count=1 -v
=== RUN   TestCollectionSubsetDelta
--- PASS: TestCollectionSubsetDelta (0.01s)
=== RUN   TestCollectionBaseTracksModuleChanges
--- PASS: TestCollectionBaseTracksModuleChanges (0.00s)
=== RUN   TestCollectionInvalidKeys
=== RUN   TestCollectionInvalidKeys/null
=== RUN   TestCollectionInvalidKeys/duplicate
=== RUN   TestCollectionInvalidKeys/wrong_type
--- PASS: TestCollectionInvalidKeys (0.00s)
=== RUN   TestCollectionEnumRequestedKeys
--- PASS: TestCollectionEnumRequestedKeys (0.00s)
=== RUN   TestCollectionTypedKeyText
...
--- PASS: TestCollectionTypedKeyText (0.00s)
=== RUN   TestCollectionDeclarations
--- PASS: TestCollectionDeclarations (0.00s)
PASS
ok  	github.com/dagger/dagger/core	0.147s

With the member check removed the test fails (An error is expected but got nil), confirming it exercises the new behavior.

go build ./core/... and go vet ./core/ are clean; gofmt reports no
diffs.

The full go test ./core/ suite was also run: the only failures are
TestNativeWorkspace*/TestGitNative*/TestImportHostCommit* git tests
that require git init --ref-format=files (host git is 2.43) plus one
pack-permission fixture — they fail identically on pristine main on this
host and are unrelated to this change.

I could not run the engine-level repro (dagger api call engine-dev test --pkg ./core/integration --run '^CollectionsSuite/TestEnumKeys$') on this
host — no Dagger engine/Docker available — so the end-to-end path is left
to CI.

Notes

Requested keys are validated against the schema-facing member names
(Local = false), since callers spell RED, while stored keys decode from
module-original names (ColorRed) through the local decoder. The
invalid enum member error text is unchanged for the valid-member-missing
case.

The key arguments of collection get and subset take the bare enum input
type, which decodes any name without checking the members. A misspelt
member therefore reached the key lookup and was reported as a key the
collection does not contain.

Convert the requested keys through the collection's enum type before the
text comparison, the same conversion collectionKeys already applies to
stored keys, so a non-member is refused with the usual invalid enum
member error that an ordinary argument produces. A valid member that is
simply absent from the current subset is still reported missing.

Signed-off-by: Santhi Prakash <[email protected]>
@santhiprakash
santhiprakash force-pushed the fix-collection-enum-keys branch from 19ce13c to 0c9691d Compare November 7, 2026 23:32
@ToniAdreal

Copy link
Copy Markdown

Verified the fix against #15101 locally: checked out this PR at 0c9691d and ran the new TestCollectionEnumRequestedKeys in core, it passes. The test covers the exact case from the issue: Red is now refused with invalid enum member "Red" for TestColor instead of the misleading "does not contain key" error, a valid member still selects normally, and a valid-but-absent member still reports "does not contain keys". The Local = false copy for the schema-exposed spelling matches the mismatch the issue describes (module-original ColorRed vs schema RED). Non-enum collections are unaffected (string keys pass through unchanged, covered by the same test).

Scope: unit-test level only, I did not run the engine-level repro from #15101.

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@santhiprakash

Copy link
Copy Markdown
Author

Still relevant — re-checked today: #15101 is open, the requested-key validation added here is still absent from current main, and the branch applies cleanly with no rebase needed (DCO green, changelog entry in place). Happy to adjust if anything has changed around module enum handling in the meantime.

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.

🐞 Collection get and subset don't check enum keys, so a misspelt key is reported as missing

2 participants