Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ module {{moduleName}}
@headers = headers
end

def_delegators :data, :[], :each, :map, :to_a, :to_hash, :dig
def_delegators :data, :[], :each, :map, :to_a, :dig

def to_h
data.respond_to?(:to_hash) ? data.to_hash : data
end

def method_missing(name, *args, &block)
return data.to_hash(*args, &block) if name == :to_hash && data.respond_to?(:to_hash)
super
end

def respond_to_missing?(name, include_private = false)
(name == :to_hash && data.respond_to?(:to_hash, include_private)) || super

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: respond_to_missing? advertises private/protected data.to_hash methods, but method_missing rejects them, so respond_to?(:to_hash, true) claims support for a call that raises. Use the same public-method check here unless private delegation is intentionally implemented.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/ruby-nextgen/response.mustache, line 29:

<comment>`respond_to_missing?` advertises private/protected `data.to_hash` methods, but `method_missing` rejects them, so `respond_to?(:to_hash, true)` claims support for a call that raises. Use the same public-method check here unless private delegation is intentionally implemented.</comment>

<file context>
@@ -14,12 +14,21 @@ module {{moduleName}}
+    end
+
+    def respond_to_missing?(name, include_private = false)
+      (name == :to_hash && data.respond_to?(:to_hash, include_private)) || super
+    end
+
</file context>
Suggested change
(name == :to_hash && data.respond_to?(:to_hash, include_private)) || super
(name == :to_hash && data.respond_to?(:to_hash)) || super

end

def deconstruct_keys(_keys)
{ data: data, status: status, headers: headers }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,27 @@ public void testResourceAccessorsUseFinalOperationIdsAndAvoidObjectMethods() thr
org.openapitools.codegen.TestUtils.assertFileContains(
target.resolve("lib/petstore/api/stables/ponies.rb"), "class Stables::Ponies");
}

@Test
public void testResponseDoesNotAdvertiseToHashWithoutHashData() throws Exception {
java.nio.file.Path target = java.nio.file.Files.createTempDirectory("test");
target.toFile().deleteOnExit();
org.openapitools.codegen.ClientOptInput input =
new org.openapitools.codegen.config.CodegenConfigurator()
.setGeneratorName("ruby-nextgen")
.setInputSpec("src/test/resources/3_0/petstore.yaml")
.setOutputDir(target.toString())
.addAdditionalProperty("gemName", "petstore")
.addAdditionalProperty("moduleName", "Petstore")
.toClientOptInput();
new org.openapitools.codegen.DefaultGenerator(false).opts(input).generate();

java.nio.file.Path response = target.resolve("lib/petstore/response.rb");
org.openapitools.codegen.TestUtils.assertFileNotContains(response,
"def_delegators :data, :[], :each, :map, :to_a, :to_hash, :dig");
org.openapitools.codegen.TestUtils.assertFileContains(response,
"def respond_to_missing?(name, include_private = false)",
"(name == :to_hash && data.respond_to?(:to_hash, include_private)) || super",
"return data.to_hash(*args, &block) if name == :to_hash && data.respond_to?(:to_hash)");
}
}
11 changes: 10 additions & 1 deletion samples/client/others/ruby-nextgen-qdrant/lib/qdrant/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ def initialize(data:, status:, headers:)
@headers = headers
end

def_delegators :data, :[], :each, :map, :to_a, :to_hash, :dig
def_delegators :data, :[], :each, :map, :to_a, :dig

def to_h
data.respond_to?(:to_hash) ? data.to_hash : data
end

def method_missing(name, *args, &block)
return data.to_hash(*args, &block) if name == :to_hash && data.respond_to?(:to_hash)
super
end

def respond_to_missing?(name, include_private = false)
(name == :to_hash && data.respond_to?(:to_hash, include_private)) || super
end

def deconstruct_keys(_keys)
{ data: data, status: status, headers: headers }
end
Expand Down
11 changes: 10 additions & 1 deletion samples/client/petstore/ruby-nextgen/lib/petstore/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ def initialize(data:, status:, headers:)
@headers = headers
end

def_delegators :data, :[], :each, :map, :to_a, :to_hash, :dig
def_delegators :data, :[], :each, :map, :to_a, :dig

def to_h
data.respond_to?(:to_hash) ? data.to_hash : data
end

def method_missing(name, *args, &block)
return data.to_hash(*args, &block) if name == :to_hash && data.respond_to?(:to_hash)
super
end

def respond_to_missing?(name, include_private = false)
(name == :to_hash && data.respond_to?(:to_hash, include_private)) || super
end

def deconstruct_keys(_keys)
{ data: data, status: status, headers: headers }
end
Expand Down
36 changes: 36 additions & 0 deletions samples/client/petstore/ruby-nextgen/spec/custom/response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Petstore::Response do
it 'does not advertise to_hash when data has none' do
response = described_class.new(data: [1], status: 200, headers: {})
expect(response.respond_to?(:to_hash)).to be(false)
expect { response.to_hash }.to raise_error(NoMethodError)
end

it 'does not advertise to_hash when data is nil' do
response = described_class.new(data: nil, status: 200, headers: {})
expect(response.respond_to?(:to_hash)).to be(false)
expect { response.to_hash }.to raise_error(NoMethodError)
end

it 'delegates to_hash when data supports it' do
response = described_class.new(data: { a: 1 }, status: 200, headers: {})
expect(response.respond_to?(:to_hash)).to be(true)
expect(response.to_hash).to eq(a: 1)
end

it 'keeps to_h working regardless of the data type' do
expect(described_class.new(data: [1, 2], status: 200, headers: {}).to_h).to eq([1, 2])
expect(described_class.new(data: { a: 1 }, status: 200, headers: {}).to_h).to eq(a: 1)
expect(described_class.new(data: nil, status: 200, headers: {}).to_h).to be_nil
end

it 'keeps the other delegators working for array data' do
response = described_class.new(data: [1, 2], status: 200, headers: {})
expect(response.map { |v| v * 2 }).to eq([2, 4])
expect(response[0]).to eq(1)
expect(response.dig(1)).to eq(2)
end
end
Loading