From 8f5ea9e55d1301a41e5df8f22758bab4a611b9ce Mon Sep 17 00:00:00 2001 From: Alexey Zapparov Date: Sun, 14 Jan 2024 17:46:10 +0100 Subject: [PATCH 1/3] fix: Escape `-` in Regexp ranges Bootsnap warns if it sees confusing regexp range like `[a-b-_]`. In general, it's better to escape dash in regexp ranges when it's supposed to mean literal dash. --- lib/openapi_client/models/bank_account.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/openapi_client/models/bank_account.rb b/lib/openapi_client/models/bank_account.rb index 77d08ac..4a33ceb 100755 --- a/lib/openapi_client/models/bank_account.rb +++ b/lib/openapi_client/models/bank_account.rb @@ -257,7 +257,7 @@ def list_invalid_properties invalid_properties.push("invalid value for \"id\", must conform to the pattern #{pattern}.") end - pattern = Regexp.new(/^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9-_]+/) + pattern = Regexp.new(/^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9\-_]+/) if !@signature_url.nil? && @signature_url !~ pattern invalid_properties.push("invalid value for \"signature_url\", must conform to the pattern #{pattern}.") end @@ -293,7 +293,7 @@ def valid? return false if @signatory.to_s.length > 30 return false if @id.nil? return false if @id !~ Regexp.new(/^bank_[a-zA-Z0-9]+$/) - return false if !@signature_url.nil? && @signature_url !~ Regexp.new(/^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9-_]+/) + return false if !@signature_url.nil? && @signature_url !~ Regexp.new(/^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9\-_]+/) return false if @date_created.nil? return false if @date_modified.nil? return false if @object.nil? @@ -392,7 +392,7 @@ def id=(id) # Custom attribute writer method with validation # @param [Object] signature_url Value to be assigned def signature_url=(signature_url) - pattern = Regexp.new(/^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9-_]+/) + pattern = Regexp.new(/^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9\-_]+/) if !signature_url.nil? && signature_url !~ pattern fail ArgumentError, "invalid value for \"signature_url\", must conform to the pattern #{pattern}." end From 1d4300cc5d96ccd2e4da4c114fadb2ecc3bf582d Mon Sep 17 00:00:00 2001 From: Alexey Zapparov Date: Sun, 14 Jan 2024 18:31:50 +0100 Subject: [PATCH 2/3] fix: Extract regexps into constants --- lib/openapi_client/models/bank_account.rb | 53 +++++++++++++---------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/openapi_client/models/bank_account.rb b/lib/openapi_client/models/bank_account.rb index 4a33ceb..1e9c97b 100755 --- a/lib/openapi_client/models/bank_account.rb +++ b/lib/openapi_client/models/bank_account.rb @@ -15,6 +15,21 @@ module Lob class BankAccount + ID_PATTERN = /^bank_[a-zA-Z0-9]+$/ + private_constant :ID_PATTERN + + SIGNATURE_URL_PATTERN = /^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9\-_]+/ + private_constant :SIGNATURE_URL_PATTERN + + DESERIALIZE_BOOLEAN_REGEXP = /\A(true|t|yes|y|1)\z/i + private_constant :DESERIALIZE_BOOLEAN_REGEXP + + DESERIALIZE_ARRAY_REGEXP = /\AArray<(?.+)>\z/ + private_constant :DESERIALIZE_ARRAY_REGEXP + + DESERIALIZE_HASH_REGEXP = /\AHash<(?.+?), (?.+)>\z/ + private_constant :DESERIALIZE_HASH_REGEXP + # An internal description that identifies this resource. Must be no longer than 255 characters. attr_accessor :description @@ -252,13 +267,11 @@ def list_invalid_properties invalid_properties.push('invalid value for "id", id cannot be nil.') end - pattern = Regexp.new(/^bank_[a-zA-Z0-9]+$/) - if @id !~ pattern - invalid_properties.push("invalid value for \"id\", must conform to the pattern #{pattern}.") + if !ID_PATTERN.match?(@id.to_s) + invalid_properties.push("invalid value for \"id\", must conform to the pattern #{ID_PATTERN}.") end - pattern = Regexp.new(/^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9\-_]+/) - if !@signature_url.nil? && @signature_url !~ pattern + if !@signature_url.nil? && !SIGNATURE_URL_PATTERN.match?(@signature_url.to_s) invalid_properties.push("invalid value for \"signature_url\", must conform to the pattern #{pattern}.") end @@ -292,8 +305,8 @@ def valid? return false if @signatory.nil? return false if @signatory.to_s.length > 30 return false if @id.nil? - return false if @id !~ Regexp.new(/^bank_[a-zA-Z0-9]+$/) - return false if !@signature_url.nil? && @signature_url !~ Regexp.new(/^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9\-_]+/) + return false if !ID_PATTERN.match?(@id.to_s) + return false if !@signature_url.nil? && !SIGNATURE_URL_PATTERN.match?(@signature_url.to_s) return false if @date_created.nil? return false if @date_modified.nil? return false if @object.nil? @@ -381,9 +394,8 @@ def id=(id) fail ArgumentError, 'id cannot be nil' end - pattern = Regexp.new(/^bank_[a-zA-Z0-9]+$/) - if id !~ pattern - fail ArgumentError, "invalid value for \"id\", must conform to the pattern #{pattern}." + if !ID_PATTERN.match?(id.to_s) + fail ArgumentError, "invalid value for \"id\", must conform to the pattern #{ID_PATTERN}." end @id = id @@ -392,9 +404,8 @@ def id=(id) # Custom attribute writer method with validation # @param [Object] signature_url Value to be assigned def signature_url=(signature_url) - pattern = Regexp.new(/^https:\/\/lob-assets.com\/(letters|postcards|bank-accounts|checks|self-mailers|cards)\/[a-z]{3,4}_[a-z0-9]{15,16}(''|_signature)(.pdf|_thumb_[a-z]+_[0-9]+.png|.png)\?(version=[a-z0-9]*&)expires=[0-9]{10}&signature=[a-zA-Z0-9\-_]+/) - if !signature_url.nil? && signature_url !~ pattern - fail ArgumentError, "invalid value for \"signature_url\", must conform to the pattern #{pattern}." + unless signature_url.nil? || SIGNATURE_URL_PATTERN.match?(signature_url.to_s) + fail ArgumentError, "invalid value for \"signature_url\", must conform to the pattern #{SIGNATURE_URL_PATTERN}." end @signature_url = signature_url @@ -499,20 +510,16 @@ def _deserialize(type, value) when :Float value.to_f when :Boolean - if value.to_s =~ /\A(true|t|yes|y|1)\z/i - true - else - false - end + DESERIALIZE_BOOLEAN_REGEXP.match?(value.to_s) when :Object # generic object (usually a Hash), return directly value - when /\AArray<(?.+)>\z/ - inner_type = Regexp.last_match[:inner_type] + when md = DESERIALIZE_ARRAY_REGEXP.match(value.to_s) + inner_type = md[:inner_type] value.map { |v| _deserialize(inner_type, v) } - when /\AHash<(?.+?), (?.+)>\z/ - k_type = Regexp.last_match[:k_type] - v_type = Regexp.last_match[:v_type] + when md = DESERIALIZE_HASH_REGEXP.match(value.to_s) + k_type = md[:k_type] + v_type = md[:v_type] {}.tap do |hash| value.each do |k, v| hash[_deserialize(k_type, k)] = _deserialize(v_type, v) From ffa1545b3c432af255a5dbdc3f3afbe50042fab6 Mon Sep 17 00:00:00 2001 From: Alexey Zapparov Date: Sun, 14 Jan 2024 18:34:15 +0100 Subject: [PATCH 3/3] fix: Update configuration specs `Lob.configure { ... }` was removed at some point, thus specs needs update. --- spec/api_client_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/api_client_spec.rb b/spec/api_client_spec.rb index b86a52e..8acba90 100644 --- a/spec/api_client_spec.rb +++ b/spec/api_client_spec.rb @@ -17,34 +17,34 @@ context 'URL stuff' do context 'host' do it 'removes http from host' do - Lob.configure { |c| c.host = 'http://example.com' } + Lob::Configuration.default.host = 'http://example.com' expect(Lob::Configuration.default.host).to eq('example.com') end it 'removes https from host' do - Lob.configure { |c| c.host = 'https://wookiee.com' } + Lob::Configuration.default.host = 'https://wookiee.com' expect(Lob::ApiClient.default.config.host).to eq('wookiee.com') end it 'removes trailing path from host' do - Lob.configure { |c| c.host = 'hobo.com/v4' } + Lob::Configuration.default.host = 'hobo.com/v4' expect(Lob::Configuration.default.host).to eq('hobo.com') end end context 'base_path' do it "prepends a slash to base_path" do - Lob.configure { |c| c.base_path = 'v4/dog' } + Lob::Configuration.default.base_path = 'v4/dog' expect(Lob::Configuration.default.base_path).to eq('/v4/dog') end it "doesn't prepend a slash if one is already there" do - Lob.configure { |c| c.base_path = '/v4/dog' } + Lob::Configuration.default.base_path = '/v4/dog' expect(Lob::Configuration.default.base_path).to eq('/v4/dog') end it "ends up as a blank string if nil" do - Lob.configure { |c| c.base_path = nil } + Lob::Configuration.default.base_path = nil expect(Lob::Configuration.default.base_path).to eq('') end end