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
13 changes: 11 additions & 2 deletions ir/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions ir/pagination_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading