Skip to content

Commit

Permalink
Merge pull request #324 from reubenmiller/feat-support-json-array-bod…
Browse files Browse the repository at this point in the history
…y-in-extensions

feat(extensions): add support for different json body types (array or object)
  • Loading branch information
reubenmiller authored Dec 6, 2023
2 parents a0c8dfd + 79b4d2d commit 9706758
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/cmdparser/cmdparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ type BodyOptions struct {
Options []flags.GetOption
IsBinary bool
Initialize bool
DefaultValue []byte
UploadProgressSource string
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/cmdparser/runtime_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ func (n *RuntimeCmd) Prepare(args []string) error {
case "formdata":
supportsFormData = true
subcmd.Body.Options = append(subcmd.Body.Options, flags.WithDataFlagValue())
case "jsonarray":
subcmd.Body.DefaultValue = []byte("[]")
subcmd.Body.Options = append(subcmd.Body.Options, flags.WithDataFlagValue())
case "jsonobject":
subcmd.Body.DefaultValue = []byte("{}")
subcmd.Body.Options = append(subcmd.Body.Options, flags.WithDataFlagValue())
default:
subcmd.Body.Options = append(subcmd.Body.Options, flags.WithDataFlagValue())
}
Expand Down Expand Up @@ -276,7 +282,12 @@ func (n *RuntimeCmd) RunE(cmd *cobra.Command, args []string) error {
}

// body
body := mapbuilder.NewInitializedMapBuilder(n.options.Body.Initialize)
var body *mapbuilder.MapBuilder
if len(n.options.Body.DefaultValue) > 0 {
body = mapbuilder.NewMapBuilderWithInit(n.options.Body.DefaultValue)
} else {
body = mapbuilder.NewInitializedMapBuilder(n.options.Body.Initialize)
}
err = flags.WithBody(
cmd,
body,
Expand Down
18 changes: 17 additions & 1 deletion tests/manual/extensions/example/body_basic_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ config:
C8Y_SETTINGS_CACHE_METHODS: GET POST PUT
C8Y_SETTINGS_DEFAULTS_CACHETTL: 100h
C8Y_SETTINGS_DEFAULTS_DRY: true
C8Y_SETTINGS_DEFAULTS_DRY_FORMAT: json
C8Y_SETTINGS_DEFAULTS_DRYFORMAT: json

tests:
#
Expand Down Expand Up @@ -286,3 +286,19 @@ tests:
stdout:
json:
body.mydata.foo.bar: "true"

jsonarray:
command: |
c8y kitchensink body jsonarray --name foo --version 1.0.0 |
c8y util show --select body -o json -c
stdout:
exactly: |
{"body":[{"name":"foo","version":"1.0.0"}]}
jsonobject:
command: |
c8y kitchensink body jsonobject --name foo --version 1.0.0 |
c8y util show --select body -o json -c
stdout:
exactly: |
{"body":{"0":{"name":"foo","version":"1.0.0"}}}
2 changes: 1 addition & 1 deletion tests/manual/extensions/example/body_cumulocity_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ config:
C8Y_SETTINGS_CACHE_METHODS: GET POST PUT
C8Y_SETTINGS_DEFAULTS_CACHETTL: 100h
C8Y_SETTINGS_DEFAULTS_DRY: true
C8Y_SETTINGS_DEFAULTS_DRY_FORMAT: json
C8Y_SETTINGS_DEFAULTS_DRYFORMAT: json

tests:
#
Expand Down
2 changes: 1 addition & 1 deletion tests/manual/extensions/example/extension_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ config:
C8Y_SETTINGS_CACHE_METHODS: GET POST PUT
C8Y_SETTINGS_DEFAULTS_CACHETTL: 100h
C8Y_SETTINGS_DEFAULTS_DRY: true
C8Y_SETTINGS_DEFAULTS_DRY_FORMAT: json
C8Y_SETTINGS_DEFAULTS_DRYFORMAT: json

tests:
It supports subcommands:
Expand Down
2 changes: 1 addition & 1 deletion tests/manual/extensions/example/extension_presets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ config:
C8Y_SETTINGS_CACHE_METHODS: GET POST PUT
C8Y_SETTINGS_DEFAULTS_CACHETTL: 100h
C8Y_SETTINGS_DEFAULTS_DRY: true
C8Y_SETTINGS_DEFAULTS_DRY_FORMAT: json
C8Y_SETTINGS_DEFAULTS_DRYFORMAT: json

tests:
# query-inventory
Expand Down
2 changes: 1 addition & 1 deletion tests/manual/extensions/example/extension_required.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ config:
C8Y_SETTINGS_CACHE_METHODS: GET POST PUT
C8Y_SETTINGS_DEFAULTS_CACHETTL: 100h
C8Y_SETTINGS_DEFAULTS_DRY: true
C8Y_SETTINGS_DEFAULTS_DRY_FORMAT: json
C8Y_SETTINGS_DEFAULTS_DRYFORMAT: json

tests:
Should return an error if missing query parameters are not provided:
Expand Down
2 changes: 1 addition & 1 deletion tests/manual/extensions/example/extension_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ config:
C8Y_SETTINGS_CACHE_METHODS: GET POST PUT
C8Y_SETTINGS_DEFAULTS_CACHETTL: 100h
C8Y_SETTINGS_DEFAULTS_DRY: true
C8Y_SETTINGS_DEFAULTS_DRY_FORMAT: json
C8Y_SETTINGS_DEFAULTS_DRYFORMAT: json

tests:
# datetime_utc
Expand Down
34 changes: 34 additions & 0 deletions tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,37 @@ commands:
- name: mydata
type: json_custom
description: json

- name: jsonarray
path: inventory/managedObjects
description: Create object
method: POST
bodyContent:
type: jsonarray
body:
- name: name
type: string
property: 0.name
description: property of an array

- name: version
type: string
property: 0.version
description: property of an array

- name: jsonobject
path: inventory/managedObjects
description: Create object
method: POST
bodyContent:
type: jsonobject
body:
- name: name
type: string
property: 0.name
description: property of an array

- name: version
type: string
property: 0.version
description: property of an array
4 changes: 3 additions & 1 deletion tools/schema/extensionCommands.json
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,9 @@
"description": "Body content type. Only used to identify binary contents",
"enum": [
"binary",
"formdata"
"formdata",
"jsonobject",
"jsonarray"
]
}
},
Expand Down

0 comments on commit 9706758

Please sign in to comment.