Skip to content

Commit be91306

Browse files
authored
fix(client): keep cache tags on pointer-slice envelopes and renamed stream entities (#133)
Two ways a generated client lost its cache contract without failing: - A Go `[]*T` reaches the document as items of `oneOf: [$ref T, {type: object, nullable: true, enum: [null]}]`, the OpenAPI 3.0 null idiom the router writes itself. compositionTarget only skipped `type: "null"`, so the null member read as an unnamed type and every pointer-slice envelope provided no tags. isNullSchema recognizes both spellings, as the codec generators already did. - resolveStreamEntityNames ran per source. A type renamed only in the OpenAPI document (it collides there, not in the AsyncAPI one) kept its bare name in the stream binding, so frames upserted `Target:7` while REST reads held `CloudprofilesTarget:7`. MergeSpecs now re-resolves bindings against the merged schemas.
1 parent 3075e62 commit be91306

4 files changed

Lines changed: 177 additions & 1 deletion

File tree

‎internal/client/entity_fields.go‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func compositionTarget(s *Schema, depth int) (string, bool) {
448448
list := false
449449

450450
for _, member := range members {
451-
if member == nil || member.Type == "null" {
451+
if member == nil || isNullSchema(member) {
452452
continue
453453
}
454454

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

468468
return found, list
469469
}
470+
471+
// isNullSchema reports whether s admits only null, in either spelling forge
472+
// documents use: OpenAPI 3.1's `type: "null"`, and the 3.0 idiom the router's
473+
// own generator writes for a Go pointer, `{type: object, nullable: true,
474+
// enum: [null]}`. Treating only the first as null made every `[]*T` envelope
475+
// look like a wrapper around an unnamed type, so it provided no cache tags.
476+
func isNullSchema(s *Schema) bool {
477+
if s == nil {
478+
return false
479+
}
480+
481+
if s.Type == "null" {
482+
return true
483+
}
484+
485+
return s.Nullable && s.Ref == "" && len(s.Enum) == 1 && s.Enum[0] == nil &&
486+
len(s.Properties) == 0 && len(s.OneOf) == 0 && len(s.AnyOf) == 0 && len(s.AllOf) == 0
487+
}

‎internal/client/envelope_infer_test.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,54 @@ func TestInferredEnvelopeYieldsToRouteOptOut(t *testing.T) {
219219
t.Fatalf("opt-out left Entity=%+v RootType=%q", ep.Entity, ep.RootType)
220220
}
221221
}
222+
223+
// A Go `[]*Order` reaches the document as an array whose items are
224+
// `oneOf: [$ref Order, {type: object, nullable: true, enum: [null]}]`, which
225+
// is how the router spells a nullable reference in OpenAPI 3.0. The null
226+
// member names nothing and must be skipped, exactly as `type: "null"` is, or
227+
// every pointer-slice envelope loses its collection tag.
228+
func TestEnvelopeOverPointerSliceProvidesCollectionTag(t *testing.T) {
229+
pointerItems := &Schema{Type: "array", Items: &Schema{OneOf: []*Schema{
230+
{Ref: "#/components/schemas/Order"},
231+
{Type: "object", Nullable: true, Enum: []any{nil}},
232+
}}}
233+
234+
for _, tc := range []struct {
235+
name string
236+
ext map[string]any
237+
}{
238+
{"declared", map[string]any{"x-forge-envelope": "orders"}},
239+
{"inferred", nil},
240+
} {
241+
t.Run(tc.name, func(t *testing.T) {
242+
spec := inferSpec(map[string]*Schema{"orders": pointerItems}, tc.ext)
243+
244+
ep := inferEP("GET")
245+
resolveEndpointCacheMeta(spec, ep, nil)
246+
247+
if ep.Entity == nil || ep.Entity.Type != "Order" {
248+
t.Fatalf("Entity = %+v, want Order", ep.Entity)
249+
}
250+
251+
if got := strings.Join(ep.CacheTags.Provides, ","); got != "Order:{id},Order[]" {
252+
t.Fatalf("provides = %q, want Order:{id},Order[]", got)
253+
}
254+
255+
if len(spec.Warnings) != 0 {
256+
t.Fatalf("warnings = %v, want none", spec.Warnings)
257+
}
258+
})
259+
}
260+
}
261+
262+
// A nullable member that carries an enum of real values is not the null
263+
// idiom, so it still makes the wrapper ambiguous.
264+
func TestNullableEnumWithValuesIsNotNull(t *testing.T) {
265+
if isNullSchema(&Schema{Type: "string", Nullable: true, Enum: []any{"a", nil}}) {
266+
t.Fatal("a nullable enum with values is not a null-only schema")
267+
}
268+
269+
if !isNullSchema(&Schema{Type: "null"}) {
270+
t.Fatal(`type "null" is null`)
271+
}
272+
}

‎internal/client/merge.go‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,70 @@ func MergeSpecs(specs ...*APISpec) *APISpec {
180180
out.WebTransports = keepFirst(out.WebTransports, "webtransport stream",
181181
func(wt WebTransportEndpoint) string { return streamKey(wt.ID, wt.Path) }, &out.Warnings)
182182

183+
renameMergedStreamEntities(out)
184+
183185
return out
184186
}
185187

188+
// renameMergedStreamEntities resolves stream binding entity names against the
189+
// merged schemas.
190+
//
191+
// Each source resolved its own bindings when it was parsed, but only against
192+
// its own components. An AsyncAPI document that never saw the collision still
193+
// names `cloudprofiles.Target` plain `Target`, while the OpenAPI document,
194+
// which also carries `planning.Target`, calls it `CloudprofilesTarget` and
195+
// marks it with x-forge-type. Until both are in one place nothing can match
196+
// the two, so a frame upserted `Target:7` while every REST read held
197+
// `CloudprofilesTarget:7`, and live updates never reached the screen.
198+
//
199+
// Bindings are copied before renaming: the slices still belong to the
200+
// sources, and a caller holding one must not see it change underneath it.
201+
func renameMergedStreamEntities(out *APISpec) {
202+
rename := func(bindings []StreamBinding) []StreamBinding {
203+
if len(bindings) == 0 {
204+
return bindings
205+
}
206+
207+
cp := make([]StreamBinding, len(bindings))
208+
for i, b := range bindings {
209+
b.Invalidates = append([]string(nil), b.Invalidates...)
210+
cp[i] = b
211+
}
212+
213+
resolveStreamEntityNames(out, cp)
214+
215+
for _, b := range cp {
216+
if b.EntityType == "" {
217+
continue
218+
}
219+
220+
if _, ok := out.Entities[b.EntityType]; ok {
221+
continue
222+
}
223+
224+
if schema, ok := out.Schemas[b.EntityType]; ok {
225+
if entity := InferEntity(out, b.EntityType, schema); entity != nil {
226+
out.Entities[b.EntityType] = entity
227+
}
228+
}
229+
}
230+
231+
return cp
232+
}
233+
234+
for i := range out.WebSockets {
235+
out.WebSockets[i].StreamBindings = rename(out.WebSockets[i].StreamBindings)
236+
}
237+
238+
for i := range out.SSEs {
239+
out.SSEs[i].StreamBindings = rename(out.SSEs[i].StreamBindings)
240+
}
241+
242+
for i := range out.WebTransports {
243+
out.WebTransports[i].StreamBindings = rename(out.WebTransports[i].StreamBindings)
244+
}
245+
}
246+
186247
// keepFirst drops every element whose identity a previous element already
187248
// claimed, warning once per dropped element. Order among the survivors is
188249
// unchanged, so the merge order established above still decides precedence.

‎internal/client/merge_test.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,49 @@ func TestMergeSpecsRequiredOrderIsIgnored(t *testing.T) {
542542
t.Errorf("required lists with the same members in a different order must not warn, got %v", got.Warnings)
543543
}
544544
}
545+
546+
// A type renamed only in the OpenAPI document (it collides there, not in the
547+
// AsyncAPI one) must still bind its frames to the REST entity, or a frame and
548+
// a read of the same record land under two different cache keys.
549+
func TestMergeSpecsRenamesStreamEntityToRESTComponent(t *testing.T) {
550+
const goType = "example.com/app/internal/clouds.Target"
551+
552+
rest := &client.APISpec{
553+
Kind: client.SourceOpenAPI,
554+
Schemas: map[string]*client.Schema{
555+
"CloudsTarget": {
556+
Type: "object",
557+
Properties: map[string]*client.Schema{"id": {Type: "string"}},
558+
Extensions: map[string]any{"x-forge-type": goType},
559+
},
560+
},
561+
Entities: map[string]*client.EntityRef{"CloudsTarget": {Type: "CloudsTarget", IDField: "id"}},
562+
}
563+
564+
bindings := []client.StreamBinding{{
565+
Message: "target.updated", EntityType: "Target", Intent: "upsert",
566+
Invalidates: []string{"Target[]"}, Type: goType,
567+
}}
568+
stream := &client.APISpec{
569+
Kind: client.SourceAsyncAPI,
570+
Schemas: map[string]*client.Schema{
571+
"Target": {Type: "object", Properties: map[string]*client.Schema{"id": {Type: "string"}}},
572+
},
573+
Entities: map[string]*client.EntityRef{"Target": {Type: "Target", IDField: "id"}},
574+
WebSockets: []client.WebSocketEndpoint{{Path: "/ws", StreamBindings: bindings}},
575+
}
576+
577+
got := client.MergeSpecs(rest, stream).WebSockets[0].StreamBindings[0]
578+
579+
if got.EntityType != "CloudsTarget" {
580+
t.Fatalf("EntityType = %q, want CloudsTarget, the name REST reads use", got.EntityType)
581+
}
582+
583+
if strings.Join(got.Invalidates, ",") != "CloudsTarget[]" {
584+
t.Fatalf("Invalidates = %v, want the derived collection tag renamed with it", got.Invalidates)
585+
}
586+
587+
if bindings[0].EntityType != "Target" || bindings[0].Invalidates[0] != "Target[]" {
588+
t.Fatalf("the source binding was mutated: %+v", bindings[0])
589+
}
590+
}

0 commit comments

Comments
 (0)