Files
magpie/internal/gateway/plain_schema_test.go
T
yetone f2ae7bdecc 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.
2026-10-31 23:35:27 +08:00

123 lines
4.3 KiB
Go

package gateway
import (
"encoding/json"
"reflect"
"testing"
)
// A $ref that points inside properties ("#/properties/…"), as dsh's tool
// parameters carry one, is a local JSON pointer like any other: resolved
// where it names, not written out as a bare object (#4561), and a
// self-reference stops at the target's type alone.
func TestPlainSchemaResolvesPointerIntoProperties(t *testing.T) {
in := `{"type":"object",
"properties":{
"func":{"type":"object","properties":{
"name":{"type":"string"},
"triggers":{"type":"array","items":{
"type":"object",
"properties":{
"kind":{"type":"string","enum":["command","prompt"]},
"self":{"$ref":"#/properties/func/properties/triggers/items"}
},
"required":["kind"]
}}
}},
"again":{"$ref":"#/properties/func/properties/triggers/items"}
}}`
want := `{"type":"object",
"properties":{
"func":{"type":"object","properties":{
"name":{"type":"string"},
"triggers":{"type":"array","items":{
"type":"object",
"properties":{
"kind":{"type":"string","enum":["command","prompt"]},
"self":{"type":"object"}
},
"required":["kind"]
}}
}},
"again":{"type":"object","properties":{
"kind":{"type":"string","enum":["command","prompt"]},
"self":{"type":"object"}
},"required":["kind"]}
}}`
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)
}
}
// 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) {
in := `{"type":"object","$schema":"http://json-schema.org/draft-07/schema#","additionalProperties":false,
"properties":{
"limit":{"type":"integer","exclusiveMinimum":0,"maximum":100,"description":"how many"},
"offset":{"type":"integer","exclusiveMinimum":0,"minimum":1},
"ratio":{"type":"number","exclusiveMaximum":1},
"url":{"type":"string","format":"uri","title":"URL","minLength":1,"pattern":"^https?://"},
"tags":{"type":"array","items":{"type":"string","uniqueItems":true},"minItems":1,"uniqueItems":true,"contains":{"type":"string"}},
"opts":{"type":"object","properties":{"deep":{"type":["boolean","null"],"default":false}},"minProperties":1,"dependentRequired":{"a":["b"]},"not":{"type":"null"}}
},"required":["limit","gone"]}`
want := `{"type":"object",
"properties":{
"limit":{"type":"integer","minimum":0,"maximum":100,"description":"how many"},
"offset":{"type":"integer","minimum":1},
"ratio":{"type":"number","maximum":1},
"url":{"type":"string","minLength":1,"pattern":"^https?://"},
"tags":{"type":"array","items":{"type":"string"},"minItems":1},
"opts":{"type":"object","properties":{"deep":{"type":"boolean","nullable":true}},"minProperties":1}
},"required":["limit"]}`
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)
}
}