gateway: plainSchema judges a self-reference by whole segments (#4561 review)
The previous commit's selfRef took a $ref as a self-reference when the
walk's path began with the ref's pointer as a string. So
"#/properties/a" inside /properties/ab, a sibling, was cut down to
{"type":"object"} instead of being resolved. A self-reference is now
the pointer itself or one of its containers: path == ref, or path
begins with ref + "/".
A "#/…" pointer that names nothing where it points also falls back to
its last segment as a $defs or definitions name again, as every ref did
before pointers were walked ("#/components/schemas/Mode" with Mode in
$defs).
TestPlainSchemaSelfRefBySegment covers both.
Broken by hand: selfRef and localRef back to the previous commit, test
kept. Red: "inner" and "mode" both go out as {"type":"object"}.
Not tried: a real Antigravity request with dsh's tool schema. The
fix is in the flattening, and the gateway tests send the result
through a fake upstream.
Run (on mote, through mote-run):
- Full `go test -tags nogui -count=1 -timeout 45m ./...` with this stack
on c30d3b7b6: 82 packages ok. internal/gui was red only on
TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll from
b09349d17) and TestColdTurnsTaken (#4510). Both are filed, and neither
touches this stack.
- Rebased onto 29d0ab39f, which changed internal/gateway since then:
gofmt -l, go vet (darwin, GOOS=linux -tags nogui, GOOS=windows) and
go test -count=1 ./internal/gateway ./internal/usage ./internal/provider
./internal/sessions all ok at the pushed head. go build ./... ok for
darwin, linux (nogui) and windows.
- go test -race of the gateway tests this stack adds or touches, on
the stack at fb29cd30f: ok.
This commit is contained in:
@@ -841,7 +841,11 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
|
||||
return nil, false
|
||||
}
|
||||
if cur == nil {
|
||||
return nil, false
|
||||
// named nowhere the pointer says: its last
|
||||
// segment as a $defs or definitions name, as
|
||||
// before pointers were walked
|
||||
d, ok := defs[ref[strings.LastIndex(ref, "/")+1:]]
|
||||
return d, ok
|
||||
}
|
||||
}
|
||||
return cur, true
|
||||
@@ -870,7 +874,10 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
|
||||
// self-reference
|
||||
selfRef := func(ref, path string) bool {
|
||||
if strings.HasPrefix(ref, "#") {
|
||||
return strings.HasPrefix(path, ref[1:])
|
||||
// whole segments: "#/properties/a" is no container of
|
||||
// /properties/ab
|
||||
r := ref[1:]
|
||||
return r == "" || path == r || strings.HasPrefix(path, r+"/")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -55,6 +55,40 @@ func TestPlainSchemaResolvesPointerIntoProperties(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A self-reference is judged by whole pointer segments: a ref to
|
||||
// "#/properties/a" inside /properties/ab is a ref to a sibling, resolved
|
||||
// whole, not stopped at its type. And a pointer that names nothing where
|
||||
// it points still finds its last segment among $defs, as it did before
|
||||
// pointers were walked (#4561 review).
|
||||
func TestPlainSchemaSelfRefBySegment(t *testing.T) {
|
||||
in := `{"type":"object",
|
||||
"$defs":{"Mode":{"type":"string","enum":["x","y"]}},
|
||||
"properties":{
|
||||
"a":{"type":"object","properties":{"n":{"type":"integer"}}},
|
||||
"ab":{"type":"object","properties":{
|
||||
"inner":{"$ref":"#/properties/a"},
|
||||
"mode":{"$ref":"#/components/schemas/Mode"}
|
||||
}}
|
||||
}}`
|
||||
want := `{"type":"object",
|
||||
"properties":{
|
||||
"a":{"type":"object","properties":{"n":{"type":"integer"}}},
|
||||
"ab":{"type":"object","properties":{
|
||||
"inner":{"type":"object","properties":{"n":{"type":"integer"}}},
|
||||
"mode":{"type":"string","enum":["x","y"]}
|
||||
}}
|
||||
}}`
|
||||
var got, exp any
|
||||
if err := json.Unmarshal(plainSchema(json.RawMessage(in)), &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
json.Unmarshal([]byte(want), &exp)
|
||||
if !reflect.DeepEqual(got, exp) {
|
||||
b, _ := json.Marshal(got)
|
||||
t.Errorf("got %s", b)
|
||||
}
|
||||
}
|
||||
|
||||
// Antigravity refuses a function declaration over any keyword outside its
|
||||
// schema subset (#187: exclusiveMinimum from zcode's and dsh's tools).
|
||||
func TestPlainSchemaKeepsOnlyWhatAntigravityTakes(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user