Skip to content

Commit

Permalink
encoding/jsonschema: always allow x- keywords
Browse files Browse the repository at this point in the history
The OpenAPI specification explicitly allows "x-"-prefixed keywords and
JSON Schema allows all unknown keywords, so it seems reasonable to
always allow them, as the `StrictKeywords` flag is more oriented towards
finding misspelled keywords than explicitly "out-of-spec" keywords.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Iaa287b25faeb3cfc03225b06b97547a949026ba7
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201112
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
rogpeppe committed Sep 12, 2024
1 parent bef55cb commit 239b405
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions encoding/jsonschema/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,13 @@ func (s *state) schemaState(n cue.Value, types cue.Kind, idRef []label, isLogica
// do multiple passes over the constraints to ensure they are done in order.
for pass := 0; pass < numPhases; pass++ {
state.processMap(n, func(key string, value cue.Value) {
if strings.HasPrefix(key, "x-") {
// A keyword starting with a leading x- is clearly
// not intended to be a valid keyword, and is explicitly
// allowed by OpenAPI. It seems reasonable that
// this is not an error even with StrictKeywords enabled.
return
}
// Convert each constraint into a either a value or a functor.
c := constraintMap[key]
if c == nil {
Expand Down
7 changes: 6 additions & 1 deletion encoding/jsonschema/testdata/txtar/strictkeywords.txtar
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#strictKeywords

Note: x-bar does _not_ cause an error even with StrictKeywords
enabled.

-- schema.json --
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "number",
"$dynamicAnchor": "bar",
"foo": true
"foo": true,
"x-bar": true
}
-- out/decode/extract --
ERROR:
Expand Down

0 comments on commit 239b405

Please sign in to comment.