From 2a86666666cbbaafe2a5fee4e5be2a591beebd57 Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Wed, 18 Nov 2026 22:33:49 +0300 Subject: [PATCH] Fix #25723: ruby-nextgen Response no longer advertises to_hash when data has none Signed-off-by: Inf1mum --- .../resources/ruby-nextgen/response.mustache | 11 +++++- .../RubyNextgenClientCodegenTest.java | 23 ++++++++++++ .../lib/qdrant/response.rb | 11 +++++- .../ruby-nextgen/lib/petstore/response.rb | 11 +++++- .../ruby-nextgen/spec/custom/response_spec.rb | 36 +++++++++++++++++++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 samples/client/petstore/ruby-nextgen/spec/custom/response_spec.rb diff --git a/modules/openapi-generator/src/main/resources/ruby-nextgen/response.mustache b/modules/openapi-generator/src/main/resources/ruby-nextgen/response.mustache index af2f97f4c9bb..71f1954dbe8f 100644 --- a/modules/openapi-generator/src/main/resources/ruby-nextgen/response.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-nextgen/response.mustache @@ -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 + end + def deconstruct_keys(_keys) { data: data, status: status, headers: headers } end diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/rubynextgen/RubyNextgenClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/rubynextgen/RubyNextgenClientCodegenTest.java index 9132e70bb442..9c9cceff6e36 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/rubynextgen/RubyNextgenClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/rubynextgen/RubyNextgenClientCodegenTest.java @@ -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)"); + } } diff --git a/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/response.rb b/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/response.rb index 6888eb6b4412..863c32d3b66b 100644 --- a/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/response.rb +++ b/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/response.rb @@ -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 diff --git a/samples/client/petstore/ruby-nextgen/lib/petstore/response.rb b/samples/client/petstore/ruby-nextgen/lib/petstore/response.rb index c0509593791c..99f577d5c485 100644 --- a/samples/client/petstore/ruby-nextgen/lib/petstore/response.rb +++ b/samples/client/petstore/ruby-nextgen/lib/petstore/response.rb @@ -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 diff --git a/samples/client/petstore/ruby-nextgen/spec/custom/response_spec.rb b/samples/client/petstore/ruby-nextgen/spec/custom/response_spec.rb new file mode 100644 index 000000000000..2e68e5f9bd21 --- /dev/null +++ b/samples/client/petstore/ruby-nextgen/spec/custom/response_spec.rb @@ -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