Skip to content

Commit 6e5c476

Browse files
authored
[Generator] Required arguments pointers shouldn't be nil (#401) (#405)
* Generator: add not nil validation for required arguments * Skip: add empty required argument tests to skip list
1 parent 17a0dc1 commit 6e5c476

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/build/cmd/generate/commands/gensource/generator.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,9 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
556556
g.w(f(g.Endpoint))
557557
} else {
558558
var (
559-
pathGrow strings.Builder
560-
pathContent strings.Builder
559+
requiredArgsValidation strings.Builder
560+
pathGrow strings.Builder
561+
pathContent strings.Builder
561562
)
562563

563564
pathGrow.WriteString(` path.Grow(`)
@@ -599,15 +600,18 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
599600
pathContent.WriteString(` path.WriteString("/")` + "\n")
600601
switch a.Type {
601602
case "int":
603+
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
602604
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
603605
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
604606
case "string":
605607
pathGrow.WriteString(`len(r.` + p + `) + `)
606608
pathContent.WriteString(` path.WriteString(r.` + p + `)` + "\n")
607609
case "list":
610+
requiredArgsValidation.WriteString(`if len(r.` + p + `) == 0 { return nil, errors.New("` + a.Name + ` is required and cannot be nil or empty") }` + "\n")
608611
pathGrow.WriteString(`len(strings.Join(r.` + p + `, ",")) + `)
609612
pathContent.WriteString(` path.WriteString(strings.Join(r.` + p + `, ","))` + "\n")
610613
case "long":
614+
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
611615
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
612616
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
613617
default:
@@ -686,6 +690,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
686690

687691
// Write out the content
688692
pathGrow.WriteString(`)`)
693+
g.w(requiredArgsValidation.String() + "\n")
689694
g.w(strings.Replace(pathGrow.String(), " + )", ")", 1) + "\n")
690695
g.w(pathContent.String() + "\n")
691696
}

internal/build/cmd/generate/commands/gentests/skips.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ cat.aliases/10_basic.yml:
121121
- "Column headers (pre 7.4.0)"
122122
- "Alias against closed index (pre 7.4.0)"
123123
124+
# Checks for nil required arguments makes this test incompatible with the integration tests
125+
indices.delete_alias/all_path_options.yml:
126+
- check delete with blank index and blank alias
127+
indices.put_alias/all_path_options.yml:
128+
- put alias with blank index
129+
- put alias with missing name
130+
124131
indices.put_mapping/10_basic.yml:
125132
- "Put mappings with explicit _doc type bwc"
126133

0 commit comments

Comments
 (0)