Skip to content

Commit c5cf7aa

Browse files
authored
🐛 Extend generator for x-no-pagination extension (#96)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Proprietary --> ### Description This change ensures the generator only generates pagination functions on collections if the `x-no-pagination` extension flag is false or non-existent. ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [X] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update). --------- Co-authored-by: Kem-Gov <[email protected]> Co-authored-by: Kem-Gov <[email protected]>
1 parent 97ea18e commit c5cf7aa

File tree

9 files changed

+76
-82
lines changed

9 files changed

+76
-82
lines changed

.secrets.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,5 @@
140140
}
141141
]
142142
},
143-
"generated_at": "2025-06-19T15:05:40Z"
143+
"generated_at": "2025-06-21T22:43:26Z"
144144
}

changes/20250621234303.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:bug: only generate pagination functions if `x-no-pagination` is false or non-existent

changes/20250622162325.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated client due to schema changes

client/extension_entities.gen.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,44 +1778,6 @@ func (o *UserCollection) FetchTitle() (string, error) {
17781778
return o.GetTitle(), nil
17791779
}
17801780

1781-
func (o *UserCollection) HasNext() bool {
1782-
if links, has := o.GetLinksOk(); has {
1783-
return links.HasNext()
1784-
}
1785-
return false
1786-
}
1787-
1788-
func (o *UserCollection) GetItemIterator() (IIterator, error) {
1789-
if o.HasEmbedded() {
1790-
embedded := o.GetEmbedded()
1791-
return NewUserIterator(embedded.GetItem())
1792-
}
1793-
links, err := o.FetchLinks()
1794-
if err != nil {
1795-
return nil, err
1796-
}
1797-
l, ok := links.(HalCollectionLinks)
1798-
if !ok {
1799-
return nil, fmt.Errorf("wrong link type [%T]; expected [HalCollectionLinks]", links)
1800-
}
1801-
return NewHalLinkDataIterator(l.GetItem())
1802-
}
1803-
1804-
func (o *UserCollection) GetItemCount() (count int64, err error) {
1805-
m, ok := o.GetMetadataOk()
1806-
if !ok {
1807-
err = fmt.Errorf("missing metadata: %v", o)
1808-
return
1809-
}
1810-
count = int64(m.GetCount())
1811-
return
1812-
}
1813-
1814-
// NewUserCollection returns a page.
1815-
func NewUserCollectionCollection() IStaticPage {
1816-
return NewUserCollectionWithDefaults()
1817-
}
1818-
18191781
// ============================================================================================
18201782
// This extends VhtInstanceItem and VhtInstanceCollection definitions
18211783
// ============================================================================================

extensions/extension_entities.gen.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,44 +1778,6 @@ func (o *UserCollection) FetchTitle() (string, error) {
17781778
return o.GetTitle(), nil
17791779
}
17801780

1781-
func (o *UserCollection) HasNext() bool {
1782-
if links, has := o.GetLinksOk(); has {
1783-
return links.HasNext()
1784-
}
1785-
return false
1786-
}
1787-
1788-
func (o *UserCollection) GetItemIterator() (IIterator, error) {
1789-
if o.HasEmbedded() {
1790-
embedded := o.GetEmbedded()
1791-
return NewUserIterator(embedded.GetItem())
1792-
}
1793-
links, err := o.FetchLinks()
1794-
if err != nil {
1795-
return nil, err
1796-
}
1797-
l, ok := links.(HalCollectionLinks)
1798-
if !ok {
1799-
return nil, fmt.Errorf("wrong link type [%T]; expected [HalCollectionLinks]", links)
1800-
}
1801-
return NewHalLinkDataIterator(l.GetItem())
1802-
}
1803-
1804-
func (o *UserCollection) GetItemCount() (count int64, err error) {
1805-
m, ok := o.GetMetadataOk()
1806-
if !ok {
1807-
err = fmt.Errorf("missing metadata: %v", o)
1808-
return
1809-
}
1810-
count = int64(m.GetCount())
1811-
return
1812-
}
1813-
1814-
// NewUserCollection returns a page.
1815-
func NewUserCollectionCollection() IStaticPage {
1816-
return NewUserCollectionWithDefaults()
1817-
}
1818-
18191781
// ============================================================================================
18201782
// This extends VhtInstanceItem and VhtInstanceCollection definitions
18211783
// ============================================================================================

generator/codegen/collection_extensions.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Collection struct {
1616
ItemRef string
1717
ModelRef string
1818
IteratorRef string
19+
NoPagination bool
1920
}
2021

2122
type Collections = []Collection
@@ -41,6 +42,24 @@ type CollectionParams = struct {
4142
NotificationFeedCollections
4243
}
4344

45+
type collectionOptions struct {
46+
noPagination bool
47+
}
48+
49+
type CollectionOption interface {
50+
apply(*collectionOptions)
51+
}
52+
53+
type noPaginationOption bool
54+
55+
func (n noPaginationOption) apply(opts *collectionOptions) {
56+
opts.noPagination = bool(n)
57+
}
58+
59+
func WithNoPagination(n bool) CollectionOption {
60+
return noPaginationOption(n)
61+
}
62+
4463
const (
4564
// Messages are a special case as they are a feed rather than a normal collection
4665
notificationFeedRef = "NotificationFeed"
@@ -207,12 +226,21 @@ func getCollectionSchema(swagger *openapi3.T, appJSON *openapi3.MediaType, endpo
207226
return
208227
}
209228

210-
func newCollection(collectionRef, itemRef string) Collection {
229+
func newCollection(collectionRef, itemRef string, opts ...CollectionOption) Collection {
230+
options := collectionOptions{
231+
noPagination: false,
232+
}
233+
234+
for _, opt := range opts {
235+
opt.apply(&options)
236+
}
237+
211238
return Collection{
212239
CollectionRef: collectionRef,
213240
ItemRef: trimRefPrefix(itemRef),
214241
ModelRef: fmt.Sprintf("%sModel", strings.TrimSuffix(collectionRef, "Collection")),
215242
IteratorRef: fmt.Sprintf("%sIterator", strings.TrimSuffix(collectionRef, "Collection")),
243+
NoPagination: options.noPagination,
216244
}
217245
}
218246

@@ -313,7 +341,17 @@ func GetCollections(swagger *openapi3.T) (collections CollectionParams, err erro
313341
continue
314342
}
315343

316-
collectionSet.Add(newCollection(collectionRef, itemRef))
344+
isNoPaginationCollection, subErr := isExtensionFlagSet(schemaVal.ExtensionProps, noPaginationFlag)
345+
if subErr != nil {
346+
err = subErr
347+
return
348+
}
349+
350+
if isNoPaginationCollection {
351+
collectionSet.Add(newCollection(collectionRef, itemRef, WithNoPagination(true)))
352+
} else {
353+
collectionSet.Add(newCollection(collectionRef, itemRef))
354+
}
317355
}
318356

319357
if isMessagesCollection := strings.HasSuffix(endpoint, "messages"); isMessagesCollection {

generator/codegen/collection_extensions_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,31 @@ func TestCollectionExtensions(t *testing.T) {
9292
require.NoError(t, err)
9393
assert.Equal(t, string(expected), string(actual))
9494
})
95+
96+
t.Run("Test Successful Generation (with x-no-pagination flag set)", func(t *testing.T) {
97+
tmpDir := t.TempDir()
98+
d, err := GenerateDataStruct(ExtensionsConfig{
99+
Input: filepath.Join("testdata", "collections", "test-x-no-pagination.json"),
100+
Template: filepath.Join("templates", "entities.go.tmpl"),
101+
Output: filepath.Join(tmpDir, "test.go"),
102+
ClientPackagePath: filepath.Join("..", "..", "client"),
103+
})
104+
require.NoError(t, err)
105+
106+
swagger, err := loadAPIDefinition(d.SpecPath)
107+
require.NoError(t, err)
108+
109+
d.Params, err = GetCollections(swagger)
110+
require.NoError(t, err)
111+
112+
err = GenerateTemplateFile(context.Background(), d)
113+
assert.NoError(t, err)
114+
assert.FileExists(t, d.DestinationPath)
115+
116+
expected, err := filesystem.ReadFile(filepath.Join("testdata", "collections", "test-x-no-pagination.gen.go"))
117+
require.NoError(t, err)
118+
actual, err := filesystem.ReadFile(d.DestinationPath)
119+
require.NoError(t, err)
120+
assert.Equal(t, string(expected), string(actual))
121+
})
95122
}

generator/codegen/common.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ const (
3333
schemaPrefix = "#/components/schemas/"
3434
jsonMIME = "application/json"
3535
// See https://github.com/Arm-Debug/API-Uniform-Contract?tab=readme-ov-file#api-extensions
36-
redactFlag = "x-redact"
37-
jobFlag = "x-job"
38-
collectionFlag = "x-collection"
36+
redactFlag = "x-redact"
37+
jobFlag = "x-job"
38+
collectionFlag = "x-collection"
39+
noPaginationFlag = "x-no-pagination"
3940
)
4041

4142
type Data struct {

generator/codegen/templates/entities.go.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func (o *{{ .CollectionRef }}) FetchTitle() (string, error) {
102102
return o.GetTitle(), nil
103103
}
104104

105+
{{ if not .NoPagination }}
105106
func (o *{{ .CollectionRef }}) HasNext() bool {
106107
if links, has := o.GetLinksOk(); has {
107108
return links.HasNext()
@@ -140,6 +141,7 @@ func New{{ .CollectionRef }}Collection() IStaticPage {
140141
return New{{ .CollectionRef }}WithDefaults()
141142
}
142143
{{ end }}
144+
{{ end }}
143145
{{ range .Params.JobItems -}}
144146
// ============================================================================================
145147
// This extends {{ .JobItemSchema }} definitions

0 commit comments

Comments
 (0)