From aa6d529ef0d5c3982769f9eb7009176f929eff71 Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Mon, 16 Sep 2024 16:02:49 +0100 Subject: [PATCH] Generator: Refactors code generation for visibility --- elasticsearch-api/utils/thor/endpoint_spec.rb | 31 +++++++++++++++++-- .../utils/thor/generate_source.rb | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/elasticsearch-api/utils/thor/endpoint_spec.rb b/elasticsearch-api/utils/thor/endpoint_spec.rb index 30a06a5562..a309cbb9e2 100644 --- a/elasticsearch-api/utils/thor/endpoint_spec.rb +++ b/elasticsearch-api/utils/thor/endpoint_spec.rb @@ -24,6 +24,23 @@ module API class EndpointSpec include EndpointSpecifics + # These APIs are private, but were added in 8.x since the generator didn't consider + # visibility. They will be removed in 9.x since we're using a different generator that + # considers visibility. But new private APIs code won't be generated for the client. + EXCLUDED_8X = [ + 'autoscaling.delete_autoscaling_policy', 'autoscaling.get_autoscaling_capacity', + 'autoscaling.get_autoscaling_policy', 'autoscaling.put_autoscaling_policy', 'capabilities', + 'connector.secret_delete', 'connector.secret_get', 'connector.secret_post', + 'connector.secret_put', 'fleet.delete_secret', 'fleet.get_secret', 'fleet.post_secret', + 'ml.validate', 'ml.validate_detector', 'monitoring.bulk', 'profiling.flamegraph', + 'profiling.stacktraces', 'profiling.status', 'profiling.topn_functions', + 'security.activate_user_profile', 'security.disable_user_profile', + 'security.enable_user_profile', 'security.get_user_profile', + 'security.has_privileges_user_profile', 'security.suggest_user_profiles', + 'security.update_user_profile_data', 'shutdown.delete_node', 'shutdown.get_node', + 'shutdown.put_node' + ].freeze + def initialize(filepath) @path = Pathname(filepath) json = MultiJson.load(File.read(@path)) @@ -31,14 +48,14 @@ def initialize(filepath) @endpoint_name = json.keys.first full_namespace = parse_full_namespace - @namespace_depth = full_namespace.size > 0 ? full_namespace.size - 1 : 0 + @namespace_depth = full_namespace.size.positive? ? full_namespace.size - 1 : 0 @module_namespace = full_namespace[0, @namespace_depth] @method_name = full_namespace.last @path_parts = parse_endpoint_parts(@spec) @params = @spec['params'] || {} @paths = @spec['url']['paths'].map { |b| b['path'] } if @spec['url'] - @path_params = path_variables.flatten.uniq.collect(&:to_sym) + @path_params = path_variables.flatten.uniq.collect(&:to_sym) @http_method = parse_http_method(@spec) @deprecation_note = @spec['url']['paths'].last&.[]('deprecated') @http_path = parse_http_path(@paths) @@ -71,6 +88,16 @@ def stability @spec['stability'] end + def visibility + @spec['visibility'] + end + + def skippable? + return true if module_namespace.flatten.first == '_internal' + + visibility != 'public' && !EXCLUDED_8X.include?(endpoint_name) + end + # Function that adds the listified h param code def specific_params super(@module_namespace.first, @method_name) diff --git a/elasticsearch-api/utils/thor/generate_source.rb b/elasticsearch-api/utils/thor/generate_source.rb index df60b0004c..acdf8bb41d 100644 --- a/elasticsearch-api/utils/thor/generate_source.rb +++ b/elasticsearch-api/utils/thor/generate_source.rb @@ -68,7 +68,7 @@ def generate_source @spec = EndpointSpec.new(filepath) say_status 'json', @spec.path, :yellow # Don't generate code for internal APIs: - next if @spec.module_namespace.flatten.first == '_internal' + next if @spec.skippable? path_to_file = output.join(@spec.module_namespace.join('/')).join("#{@spec.method_name}.rb") dir = output.join(@spec.module_namespace.join('/'))