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.

Also, use `StrictFeatures` rather than `StrictKeywords` in the external
test, because that corresponds better to the spec that the tests are
written against.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Iaa287b25faeb3cfc03225b06b97547a949026ba7
Dispatch-Trailer: {"type":"trybot","CL":1201112,"patchset":6,"ref":"refs/changes/12/1201112/6","targetBranch":"master"}
  • Loading branch information
rogpeppe authored and cueckoo committed Sep 12, 2024
1 parent 73dabdb commit 6f992be
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 6f992be

Please sign in to comment.