From cd062b066962bcec1bbe3161f780efdacffb69a3 Mon Sep 17 00:00:00 2001 From: Oleh Fedorenko Date: Wed, 11 Dec 2024 15:50:19 +0000 Subject: [PATCH] Fixes #38077 - Don't build hidden params as options --- lib/hammer_cli/apipie/option_builder.rb | 2 ++ lib/hammer_cli/options/option_definition.rb | 6 ++++++ test/unit/apipie/option_builder_test.rb | 10 ++++++++++ test/unit/fixtures/apipie/documented.json | 20 ++++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/lib/hammer_cli/apipie/option_builder.rb b/lib/hammer_cli/apipie/option_builder.rb index 07378cd8..5a798d69 100644 --- a/lib/hammer_cli/apipie/option_builder.rb +++ b/lib/hammer_cli/apipie/option_builder.rb @@ -30,6 +30,7 @@ def option(*args) def options_for_params(params, filter, resource_name_map, opts = {}) options = [] params.each do |p| + next unless p.show? exists = opts[:command].find_option(option_switch(p, resource_name_map)) next if filter.include?(p.name) || filter.include?(p.name.to_sym) || exists @@ -85,6 +86,7 @@ def option_opts(param, resource_name_map) opts[:attribute_name] = HammerCLI.option_accessor_name(param.name) opts[:referenced_resource] = resource_name(param) opts[:aliased_resource] = aliased_name(resource_name(param), resource_name_map) + opts[:show] = param.show? return opts end diff --git a/lib/hammer_cli/options/option_definition.rb b/lib/hammer_cli/options/option_definition.rb index 2ef1fa13..92c7d56e 100644 --- a/lib/hammer_cli/options/option_definition.rb +++ b/lib/hammer_cli/options/option_definition.rb @@ -30,6 +30,8 @@ def initialize(switches, type, description, options = {}) @context_target = options[:context_target] @deprecated_switches = options[:deprecated] @family = options[:family] + # We expect a value from API param docs, but in case it's not there, we want to show it in help by default + @show = options.fetch(:show, true) super end @@ -153,6 +155,10 @@ def child? @family.children.include?(self) end + def show? + @show + end + private def format_deprecation_msg(option_desc, deprecation_msg) diff --git a/test/unit/apipie/option_builder_test.rb b/test/unit/apipie/option_builder_test.rb index 57df2ce0..99101ae1 100644 --- a/test/unit/apipie/option_builder_test.rb +++ b/test/unit/apipie/option_builder_test.rb @@ -178,4 +178,14 @@ end end + context "hidden parameters" do + let(:action) { resource.action(:create) } + + it "should skip options for params with show? false" do + option_switches = options.map(&:long_switch) + refute_includes(option_switches, "--hidden") + refute_includes(option_switches, "--documented-scope") + end + end + end diff --git a/test/unit/fixtures/apipie/documented.json b/test/unit/fixtures/apipie/documented.json index 526a1528..eb48c2f8 100644 --- a/test/unit/fixtures/apipie/documented.json +++ b/test/unit/fixtures/apipie/documented.json @@ -75,6 +75,16 @@ "examples": [], "errors": [], "params": [ + { + "allow_null": false, + "name": "documented_scope", + "full_name": "documented_scope", + "validator": "Must be String", + "description": "", + "expected_type": "string", + "required": false, + "show": false + }, { "allow_null": false, "name": "documented", @@ -110,6 +120,16 @@ "expected_type": "array", "description": "", "required": true + }, + { + "name": "hidden", + "allow_null": false, + "full_name": "documented[hidden]", + "validator": "Must be number", + "expected_type": "number", + "description": "", + "required": false, + "show": false } ] }