Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion internal/client/entity_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func compositionTarget(s *Schema, depth int) (string, bool) {
list := false

for _, member := range members {
if member == nil || member.Type == "null" {
if member == nil || isNullSchema(member) {
continue
}

Expand All @@ -467,3 +467,21 @@ func compositionTarget(s *Schema, depth int) (string, bool) {

return found, list
}

// isNullSchema reports whether s admits only null, in either spelling forge
// documents use: OpenAPI 3.1's `type: "null"`, and the 3.0 idiom the router's
// own generator writes for a Go pointer, `{type: object, nullable: true,
// enum: [null]}`. Treating only the first as null made every `[]*T` envelope
// look like a wrapper around an unnamed type, so it provided no cache tags.
func isNullSchema(s *Schema) bool {
if s == nil {
return false
}

if s.Type == "null" {
return true
}

return s.Nullable && s.Ref == "" && len(s.Enum) == 1 && s.Enum[0] == nil &&
len(s.Properties) == 0 && len(s.OneOf) == 0 && len(s.AnyOf) == 0 && len(s.AllOf) == 0
}
51 changes: 51 additions & 0 deletions internal/client/envelope_infer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,54 @@ func TestInferredEnvelopeYieldsToRouteOptOut(t *testing.T) {
t.Fatalf("opt-out left Entity=%+v RootType=%q", ep.Entity, ep.RootType)
}
}

// A Go `[]*Order` reaches the document as an array whose items are
// `oneOf: [$ref Order, {type: object, nullable: true, enum: [null]}]`, which
// is how the router spells a nullable reference in OpenAPI 3.0. The null
// member names nothing and must be skipped, exactly as `type: "null"` is, or
// every pointer-slice envelope loses its collection tag.
func TestEnvelopeOverPointerSliceProvidesCollectionTag(t *testing.T) {
pointerItems := &Schema{Type: "array", Items: &Schema{OneOf: []*Schema{
{Ref: "#/components/schemas/Order"},
{Type: "object", Nullable: true, Enum: []any{nil}},
}}}

for _, tc := range []struct {
name string
ext map[string]any
}{
{"declared", map[string]any{"x-forge-envelope": "orders"}},
{"inferred", nil},
} {
t.Run(tc.name, func(t *testing.T) {
spec := inferSpec(map[string]*Schema{"orders": pointerItems}, tc.ext)

ep := inferEP("GET")
resolveEndpointCacheMeta(spec, ep, nil)

if ep.Entity == nil || ep.Entity.Type != "Order" {
t.Fatalf("Entity = %+v, want Order", ep.Entity)
}

if got := strings.Join(ep.CacheTags.Provides, ","); got != "Order:{id},Order[]" {
t.Fatalf("provides = %q, want Order:{id},Order[]", got)
}

if len(spec.Warnings) != 0 {
t.Fatalf("warnings = %v, want none", spec.Warnings)
}
})
}
}

// A nullable member that carries an enum of real values is not the null
// idiom, so it still makes the wrapper ambiguous.
func TestNullableEnumWithValuesIsNotNull(t *testing.T) {
if isNullSchema(&Schema{Type: "string", Nullable: true, Enum: []any{"a", nil}}) {
t.Fatal("a nullable enum with values is not a null-only schema")
}

if !isNullSchema(&Schema{Type: "null"}) {
t.Fatal(`type "null" is null`)
}
}
61 changes: 61 additions & 0 deletions internal/client/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,70 @@ func MergeSpecs(specs ...*APISpec) *APISpec {
out.WebTransports = keepFirst(out.WebTransports, "webtransport stream",
func(wt WebTransportEndpoint) string { return streamKey(wt.ID, wt.Path) }, &out.Warnings)

renameMergedStreamEntities(out)

return out
}

// renameMergedStreamEntities resolves stream binding entity names against the
// merged schemas.
//
// Each source resolved its own bindings when it was parsed, but only against
// its own components. An AsyncAPI document that never saw the collision still
// names `cloudprofiles.Target` plain `Target`, while the OpenAPI document,
// which also carries `planning.Target`, calls it `CloudprofilesTarget` and
// marks it with x-forge-type. Until both are in one place nothing can match
// the two, so a frame upserted `Target:7` while every REST read held
// `CloudprofilesTarget:7`, and live updates never reached the screen.
//
// Bindings are copied before renaming: the slices still belong to the
// sources, and a caller holding one must not see it change underneath it.
func renameMergedStreamEntities(out *APISpec) {
rename := func(bindings []StreamBinding) []StreamBinding {
if len(bindings) == 0 {
return bindings
}

cp := make([]StreamBinding, len(bindings))
for i, b := range bindings {
b.Invalidates = append([]string(nil), b.Invalidates...)
cp[i] = b
}

resolveStreamEntityNames(out, cp)

for _, b := range cp {
if b.EntityType == "" {
continue
}

if _, ok := out.Entities[b.EntityType]; ok {
continue
}

if schema, ok := out.Schemas[b.EntityType]; ok {
if entity := InferEntity(out, b.EntityType, schema); entity != nil {
out.Entities[b.EntityType] = entity
}
}
}

return cp
}

for i := range out.WebSockets {
out.WebSockets[i].StreamBindings = rename(out.WebSockets[i].StreamBindings)
}

for i := range out.SSEs {
out.SSEs[i].StreamBindings = rename(out.SSEs[i].StreamBindings)
}

for i := range out.WebTransports {
out.WebTransports[i].StreamBindings = rename(out.WebTransports[i].StreamBindings)
}
}

// keepFirst drops every element whose identity a previous element already
// claimed, warning once per dropped element. Order among the survivors is
// unchanged, so the merge order established above still decides precedence.
Expand Down
46 changes: 46 additions & 0 deletions internal/client/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,49 @@ func TestMergeSpecsRequiredOrderIsIgnored(t *testing.T) {
t.Errorf("required lists with the same members in a different order must not warn, got %v", got.Warnings)
}
}

// A type renamed only in the OpenAPI document (it collides there, not in the
// AsyncAPI one) must still bind its frames to the REST entity, or a frame and
// a read of the same record land under two different cache keys.
func TestMergeSpecsRenamesStreamEntityToRESTComponent(t *testing.T) {
const goType = "example.com/app/internal/clouds.Target"

rest := &client.APISpec{
Kind: client.SourceOpenAPI,
Schemas: map[string]*client.Schema{
"CloudsTarget": {
Type: "object",
Properties: map[string]*client.Schema{"id": {Type: "string"}},
Extensions: map[string]any{"x-forge-type": goType},
},
},
Entities: map[string]*client.EntityRef{"CloudsTarget": {Type: "CloudsTarget", IDField: "id"}},
}

bindings := []client.StreamBinding{{
Message: "target.updated", EntityType: "Target", Intent: "upsert",
Invalidates: []string{"Target[]"}, Type: goType,
}}
stream := &client.APISpec{
Kind: client.SourceAsyncAPI,
Schemas: map[string]*client.Schema{
"Target": {Type: "object", Properties: map[string]*client.Schema{"id": {Type: "string"}}},
},
Entities: map[string]*client.EntityRef{"Target": {Type: "Target", IDField: "id"}},
WebSockets: []client.WebSocketEndpoint{{Path: "/ws", StreamBindings: bindings}},
}

got := client.MergeSpecs(rest, stream).WebSockets[0].StreamBindings[0]

if got.EntityType != "CloudsTarget" {
t.Fatalf("EntityType = %q, want CloudsTarget, the name REST reads use", got.EntityType)
}

if strings.Join(got.Invalidates, ",") != "CloudsTarget[]" {
t.Fatalf("Invalidates = %v, want the derived collection tag renamed with it", got.Invalidates)
}

if bindings[0].EntityType != "Target" || bindings[0].Invalidates[0] != "Target[]" {
t.Fatalf("the source binding was mutated: %+v", bindings[0])
}
}
Loading