Skip to content

Commit 54d900e

Browse files
authored
minor refactoring (exoego#323)
1 parent 931852d commit 54d900e

3 files changed

Lines changed: 23 additions & 24 deletions

File tree

lib/rspec/openapi/components_updater.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative 'hash_helper'
44

55
class << RSpec::OpenAPI::ComponentsUpdater = Object.new
6+
SCHEMA_REF_PREFIX = '#/components/schemas/'
7+
68
# @param [Hash] base
79
# @param [Hash] fresh
810
def update!(base, fresh)
@@ -30,7 +32,7 @@ def update!(base, fresh)
3032
# Skip if the property using $ref is not found in the parent schema. The property may be removed.
3133
next if nested_schema.nil?
3234

33-
schema_name = base.dig(*paths)&.gsub('#/components/schemas/', '')&.to_sym
35+
schema_name = extract_schema_name(base.dig(*paths))&.to_sym
3436
fresh_schemas[schema_name] ||= {}
3537
RSpec::OpenAPI::SchemaMerger.merge!(fresh_schemas[schema_name], nested_schema)
3638
end
@@ -44,7 +46,7 @@ def update!(base, fresh)
4446
def build_fresh_schemas(references, base, fresh)
4547
references.inject({}) do |acc, paths|
4648
ref_link = dig_schema(base, paths)[:$ref]
47-
schema_name = ref_link.to_s.gsub('#/components/schemas/', '')
49+
schema_name = extract_schema_name(ref_link)
4850
schema_body = dig_schema(fresh, paths.grep_v(Integer))
4951

5052
RSpec::OpenAPI::SchemaMerger.merge!(acc, { schema_name => schema_body })
@@ -81,7 +83,7 @@ def find_non_top_level_nested_refs(base, generated_names)
8183
# Reject already-generated schemas to reduce unnecessary loop
8284
nested_refs.reject do |paths|
8385
ref_link = base.dig(*paths)
84-
schema_name = ref_link.gsub('#/components/schemas/', '')
86+
schema_name = extract_schema_name(ref_link)
8587
generated_names.include?(schema_name)
8688
end
8789
end
@@ -91,11 +93,19 @@ def find_one_of_refs(base, paths)
9193
return unless one_of
9294

9395
one_of.each_with_index.filter_map do |schema, index|
94-
paths + [index] if schema&.dig(:$ref)&.start_with?('#/components/schemas/')
96+
paths + [index] if schema_ref?(schema&.dig(:$ref))
9597
end
9698
end
9799

98100
def find_object_refs(base, paths)
99-
[paths] if dig_schema(base, paths)&.dig(:$ref)&.start_with?('#/components/schemas/')
101+
[paths] if schema_ref?(dig_schema(base, paths)&.dig(:$ref))
102+
end
103+
104+
def extract_schema_name(ref_link)
105+
ref_link&.delete_prefix(SCHEMA_REF_PREFIX)
106+
end
107+
108+
def schema_ref?(ref_link)
109+
ref_link&.start_with?(SCHEMA_REF_PREFIX)
100110
end
101111
end

lib/rspec/openapi/extractors/hanami.rb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ def request_attributes(request, example)
6161

6262
raw_path_params = route.params
6363

64-
result = InspectorAnalyzer.call(request.method, add_id(path, route))
64+
result = InspectorAnalyzer.call(request.method, replace_path_params(path, route, '/:%{key}'))
6565

6666
summary ||= result[:summary]
6767
tags ||= result[:tags]
68-
path = add_openapi_id(path, route)
68+
path = replace_path_params(path, route, '/{%{key}}')
6969

7070
raw_path_params = raw_path_params.slice(*(raw_path_params.keys - RSpec::OpenAPI.ignored_path_params))
7171

@@ -99,21 +99,11 @@ def request_response(context)
9999

100100
private
101101

102-
def add_id(path, route)
102+
def replace_path_params(path, route, format)
103103
return path if route.params.empty?
104104

105105
route.params.each_pair do |key, value|
106-
path = path.sub("/#{value}", "/:#{key}")
107-
end
108-
109-
path
110-
end
111-
112-
def add_openapi_id(path, route)
113-
return path if route.params.empty?
114-
115-
route.params.each_pair do |key, value|
116-
path = path.sub("/#{value}", "/{#{key}}")
106+
path = path.sub("/#{value}", format % { key: key })
117107
end
118108

119109
path

lib/rspec/openapi/schema_builder.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ def infer_enum(path, record, context)
270270
enum_hash = context == :request ? record.request_enum : record.response_enum
271271
return nil unless enum_hash
272272

273-
# Try both string and symbol keys
274-
enum_hash[path.to_s] || enum_hash[path.to_sym]
273+
# Keys are already normalized to strings by SharedExtractor.normalize_enum
274+
enum_hash[path.to_s]
275275
end
276276

277277
# Convert an always-String param to an appropriate type
@@ -319,9 +319,8 @@ def normalize_content_type(content_type)
319319
content_type&.sub(/;.+\z/, '')
320320
end
321321

322-
def normalize_content_disposition(content_disposition)
323-
content_disposition&.sub(/;.+\z/, '')
324-
end
322+
# Same logic as normalize_content_type – strips header parameters after ';'
323+
alias normalize_content_disposition normalize_content_type
325324

326325
def build_array_items_schema(array, record: nil, path: nil, context: nil)
327326
return {} if array.empty?

0 commit comments

Comments
 (0)