From c6840933b216bf6e74d37bf8ae91883d4fbce41c Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Wed, 6 Dec 2023 16:02:35 +0100 Subject: [PATCH 1/2] feat(extensions): add support for controlling the json body type, e.g. array or object --- pkg/cmdparser/cmdparser.go | 1 + pkg/cmdparser/runtime_command.go | 13 ++++++- .../extensions/example/body_basic_tests.yaml | 16 +++++++++ .../c8y-kitchensink/api/body_basic.yaml | 34 +++++++++++++++++++ tools/schema/extensionCommands.json | 4 ++- 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/pkg/cmdparser/cmdparser.go b/pkg/cmdparser/cmdparser.go index 9cd068bc3..9a778a67c 100644 --- a/pkg/cmdparser/cmdparser.go +++ b/pkg/cmdparser/cmdparser.go @@ -724,6 +724,7 @@ type BodyOptions struct { Options []flags.GetOption IsBinary bool Initialize bool + DefaultValue []byte UploadProgressSource string } diff --git a/pkg/cmdparser/runtime_command.go b/pkg/cmdparser/runtime_command.go index a026dbfb3..52810d59c 100644 --- a/pkg/cmdparser/runtime_command.go +++ b/pkg/cmdparser/runtime_command.go @@ -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()) } @@ -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, diff --git a/tests/manual/extensions/example/body_basic_tests.yaml b/tests/manual/extensions/example/body_basic_tests.yaml index 87219a4ea..569fd666a 100644 --- a/tests/manual/extensions/example/body_basic_tests.yaml +++ b/tests/manual/extensions/example/body_basic_tests.yaml @@ -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"}}} diff --git a/tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml b/tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml index cd704bc88..e700fb14e 100644 --- a/tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml +++ b/tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml @@ -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 diff --git a/tools/schema/extensionCommands.json b/tools/schema/extensionCommands.json index c8ea2f557..628c50746 100644 --- a/tools/schema/extensionCommands.json +++ b/tools/schema/extensionCommands.json @@ -592,7 +592,9 @@ "description": "Body content type. Only used to identify binary contents", "enum": [ "binary", - "formdata" + "formdata", + "jsonobject", + "jsonarray" ] } }, From 79b4d2d0a89067b1fde08620e414181371f0eb6b Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Wed, 6 Dec 2023 17:53:15 +0100 Subject: [PATCH 2/2] fix invalid reference to dry format env variable --- tests/manual/extensions/example/body_basic_tests.yaml | 2 +- tests/manual/extensions/example/body_cumulocity_tests.yaml | 2 +- tests/manual/extensions/example/extension_example.yaml | 2 +- tests/manual/extensions/example/extension_presets.yaml | 2 +- tests/manual/extensions/example/extension_required.yaml | 2 +- tests/manual/extensions/example/extension_types.yaml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/manual/extensions/example/body_basic_tests.yaml b/tests/manual/extensions/example/body_basic_tests.yaml index 569fd666a..57cb9711e 100644 --- a/tests/manual/extensions/example/body_basic_tests.yaml +++ b/tests/manual/extensions/example/body_basic_tests.yaml @@ -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: # diff --git a/tests/manual/extensions/example/body_cumulocity_tests.yaml b/tests/manual/extensions/example/body_cumulocity_tests.yaml index 3f5a06ada..d0d3b0ae4 100644 --- a/tests/manual/extensions/example/body_cumulocity_tests.yaml +++ b/tests/manual/extensions/example/body_cumulocity_tests.yaml @@ -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: # diff --git a/tests/manual/extensions/example/extension_example.yaml b/tests/manual/extensions/example/extension_example.yaml index 8a5775344..f12850d64 100644 --- a/tests/manual/extensions/example/extension_example.yaml +++ b/tests/manual/extensions/example/extension_example.yaml @@ -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: diff --git a/tests/manual/extensions/example/extension_presets.yaml b/tests/manual/extensions/example/extension_presets.yaml index e37da58f0..307335770 100644 --- a/tests/manual/extensions/example/extension_presets.yaml +++ b/tests/manual/extensions/example/extension_presets.yaml @@ -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 diff --git a/tests/manual/extensions/example/extension_required.yaml b/tests/manual/extensions/example/extension_required.yaml index edbd46b05..e9af02816 100644 --- a/tests/manual/extensions/example/extension_required.yaml +++ b/tests/manual/extensions/example/extension_required.yaml @@ -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: diff --git a/tests/manual/extensions/example/extension_types.yaml b/tests/manual/extensions/example/extension_types.yaml index 5ca57650b..c1d101b58 100644 --- a/tests/manual/extensions/example/extension_types.yaml +++ b/tests/manual/extensions/example/extension_types.yaml @@ -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