Repository navigation
fix(gen): omit unset flattened-oneOf struct fields from requests - #88
Merged
Merged
Conversation
isStructSchema classified a $ref to a oneOf of object arms by its raw
schema and returned false, but emitModels emits such a schema as one
flattened struct. An optional request field of that type therefore got
`,omitempty`, which never drops a struct value, so an unset field was
sent as an object holding only an empty discriminator.
DashboardVariable.VariableQuery was affected: every datasource or custom
variable went out with "variable_query":{"kind":""}, and the API rejected
dashboard create, update and panel preview with
`unknown field "variable_query"`.
Classify the resolved (flattened) schema instead, which gives the field
`,omitzero`. Regenerating changes only that one tag.
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
DashboardVariableis one flattened struct covering thedatasource,customandqueryarms. Only thequeryarm carriesvariable_query, but the generated tag was:,omitemptynever drops a struct value, so every datasource or custom variable was sent as:{"kind": "datasource", "name": "metrics_ds", "variable_query": {"kind": ""}}The API validates each arm strictly, so it rejected
/monit/dashboard/create,/monit/dashboard/updateand/monit/dashboard/panel/previewwithunknown field "variable_query". That covers any dashboard that has a datasource or custom variable.Cause
isStructSchemaresolves a$refby looking at the raw target schema. ForDashboardVariableQuerythat schema is aoneOf, so the function returnedfalse.emitModels, however, emits a named oneOf of object arms as one flattened struct (resolveObject→mergeAllOf). Because the field was misclassified, it skipped the existing,omitzerorule for optional struct-typed request fields.Fix
Classify the
$reftarget the same wayemitModelsemits it: run it throughresolveObjectfirst. InlineoneOfschemas still returnfalse;goTypeOfemits those asany.Regenerating changes exactly one line in
models_gen.go: thevariable_querytag goes fromomitemptytoomitzero. No types change and nothing becomes a pointer, so this is not a breaking change.Tests
TestEmitStructOptionalFlattenedOneOfRequestFieldOmitsZero(generator): an optional request field whose$reftarget is a oneOf of objects is emitted with,omitzero.TestDashboardVariableOmitsUnsetVariableQuery(SDK): a datasource variable marshals withoutvariable_query, and a query variable still includes it.Both tests fail on
mainand pass with this change.go generate ./...reproduces the committed output,golangci-lint runreports 0 issues, andgo test ./...passes.I also built the CLI against this branch and ran it end to end against a test account, using a dashboard with a datasource variable. Panel preview, create, get and delete all succeeded, and the stored variable has no
variable_querykey.