diff --git a/ir/pagination.go b/ir/pagination.go index 5c675685..bfa4e6ba 100644 --- a/ir/pagination.go +++ b/ir/pagination.go @@ -124,7 +124,16 @@ func pagination(op *Operation, byName map[string]*Schema) (*Pagination, error) { } items := fieldOf(list, ext.Items, itemsNames) - if items == nil || !strings.HasPrefix(items.Type, "[]") { + if items == nil { + return nil, fmt.Errorf("%s has no list of items", list.Name) + } + + itemsType := items.Type + if s := byName[itemsType]; s != nil && s.Kind == SchemaKindAlias { + itemsType = s.Type // a named list, as type Templates []Template + } + + if !strings.HasPrefix(itemsType, "[]") { return nil, fmt.Errorf("%s has no list of items", list.Name) } @@ -139,7 +148,7 @@ func pagination(op *Operation, byName map[string]*Schema) (*Pagination, error) { } p := &Pagination{ - CursorType: next.Type, NextField: next.Name, ItemsField: items.Name, ItemType: strings.TrimPrefix(items.Type, "[]"), + CursorType: next.Type, NextField: next.Name, ItemsField: items.Name, ItemType: strings.TrimPrefix(itemsType, "[]"), } if more != nil { p.MoreField = more.Name diff --git a/ir/pagination_internal_test.go b/ir/pagination_internal_test.go index c33ddef0..ccfccd36 100644 --- a/ir/pagination_internal_test.go +++ b/ir/pagination_internal_test.go @@ -81,6 +81,20 @@ func TestPaginate(t *testing.T) { t.Errorf("x-pagination false: got %+v, %v; want none", ops[0].Pagination, err) } + // a named list of items is followed to its items + named := []Schema{ + {Name: "L", Kind: SchemaKindStruct, Fields: []Field{ + {Name: "Things", JSONName: "things", Type: "Things"}, + {Name: "NextCursor", JSONName: "next_cursor", Type: "string"}, + }}, + {Name: "Things", Kind: SchemaKindAlias, Type: "[]Thing"}, + } + + ops = []Operation{listOp("string", paginationExt{set: true, Items: "things"})} + if err := paginate(ops, named); err != nil || ops[0].Pagination == nil || ops[0].Pagination.ItemType != "Thing" { + t.Errorf("a named list: got %+v, %v; want items of Thing", ops[0].Pagination, err) + } + ops = []Operation{listOp("string", paginationExt{}), {Name: "ListThingsAll"}} if err := paginate(ops, listSchemas); err != nil || ops[0].Pagination != nil { t.Errorf("a name taken: got %+v, %v; want none", ops[0].Pagination, err)