Skip to content

Commit 0418c5c

Browse files
authored
{sls} Add parameter missing check for path/host and array response JSON serialization (#1275)
* add check for path and host * remove no use
1 parent dce7366 commit 0418c5c

File tree

2 files changed

+467
-2
lines changed

2 files changed

+467
-2
lines changed

openapi/http_context.go

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func GetContentFromApiResponse(response map[string]any) string {
9696
switch v := responseBody.(type) {
9797
case string:
9898
out = v
99-
case map[string]any:
99+
case map[string]any, []any:
100100
jsonData, _ := json.Marshal(v)
101101
out = string(jsonData)
102102
case []byte:
@@ -368,6 +368,66 @@ func (a *OpenapiContext) Prepare(ctx *cli.Context) error {
368368
return a.RequestProcessors(ctx)
369369
}
370370

371+
func (a *OpenapiContext) checkRequiredParameters(ctx *cli.Context) error {
372+
// 收集所有 required 的 Path 和 Host 参数
373+
requiredPathParams := make(map[string]bool)
374+
requiredHostParams := make(map[string]bool)
375+
376+
for _, param := range a.api.Parameters {
377+
if param.Position == "Host" && param.Required {
378+
requiredHostParams[param.Name] = false
379+
}
380+
if param.Position == "Path" && param.Required {
381+
requiredPathParams[param.Name] = false
382+
}
383+
}
384+
385+
for _, f := range ctx.UnknownFlags().Flags() {
386+
param := a.api.FindParameter(f.Name)
387+
if param == nil {
388+
continue
389+
}
390+
if param.Position == "Host" && param.Required {
391+
value, _ := f.GetValue()
392+
if value != "" {
393+
requiredHostParams[param.Name] = true
394+
}
395+
}
396+
if param.Position == "Path" && param.Required {
397+
value, _ := f.GetValue()
398+
if value != "" {
399+
requiredPathParams[param.Name] = true
400+
}
401+
}
402+
}
403+
404+
allMissing := make([]string, 0)
405+
for paramName, filled := range requiredHostParams {
406+
if !filled {
407+
prefix := "--"
408+
if len(paramName) == 1 {
409+
prefix = "-"
410+
}
411+
allMissing = append(allMissing, fmt.Sprintf("host parameter %s%s", prefix, paramName))
412+
}
413+
}
414+
for paramName, filled := range requiredPathParams {
415+
if !filled {
416+
prefix := "--"
417+
if len(paramName) == 1 {
418+
prefix = "-"
419+
}
420+
allMissing = append(allMissing, fmt.Sprintf("path parameter %s%s", prefix, paramName))
421+
}
422+
}
423+
424+
if len(allMissing) > 0 {
425+
return fmt.Errorf("required parameters missing: %s", strings.Join(allMissing, ", "))
426+
}
427+
428+
return nil
429+
}
430+
371431
func (a *OpenapiContext) RequestProcessors(ctx *cli.Context) error {
372432
processors := []Processor{
373433
a.ProcessHeaders,
@@ -382,7 +442,7 @@ func (a *OpenapiContext) RequestProcessors(ctx *cli.Context) error {
382442
return err
383443
}
384444
}
385-
return nil
445+
return a.checkRequiredParameters(ctx)
386446
}
387447

388448
func (a *OpenapiContext) CheckResponseForPullLogs(response map[string]any) (string, error) {

0 commit comments

Comments
 (0)