diff --git a/modules/openapi-generator/src/main/resources/ruby-nextgen/configuration.mustache b/modules/openapi-generator/src/main/resources/ruby-nextgen/configuration.mustache index 2a1694b2b6b1..09d2c1f38dd1 100644 --- a/modules/openapi-generator/src/main/resources/ruby-nextgen/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-nextgen/configuration.mustache @@ -31,7 +31,6 @@ module {{moduleName}} conn.request :multipart conn.request :url_encoded conn.request :json - conn.response :json, content_type: /\bjson$/ conn.options.timeout = timeout conn.response :logger, logger if logger && debugging @middlewares.each { |mw, mw_args, mw_block| conn.use(mw, *mw_args, &mw_block) } 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..463d82089b29 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 @@ -52,6 +52,26 @@ public void testObjectQueryParametersRespectStyleAndExplode() throws Exception { + " }.compact].reduce({}, :merge)"); } + @Test + public void testJsonStringBodyResponseIsDeserializedOnce() throws Exception { + Path target = Files.createTempDirectory("test"); + target.toFile().deleteOnExit(); + ClientOptInput input = + new CodegenConfigurator() + .setGeneratorName("ruby-nextgen") + .setInputSpec("src/test/resources/3_0/ruby-nextgen/json-string-body.yaml") + .setOutputDir(target.toString()) + .addAdditionalProperty("gemName", "petstore") + .addAdditionalProperty("moduleName", "Petstore") + .toClientOptInput(); + new DefaultGenerator(false).opts(input).generate(); + + TestUtils.assertFileNotContains( + target.resolve("lib/petstore/configuration.rb"), + "conn.response :json"); + TestUtils.assertFileContains(target.resolve("lib/petstore/connection.rb"), "JSON.parse(body)"); + } + @Test public void testGeneratorName() { RubyNextgenClientCodegen gen = new RubyNextgenClientCodegen(); diff --git a/modules/openapi-generator/src/test/resources/3_0/ruby-nextgen/json-string-body.yaml b/modules/openapi-generator/src/test/resources/3_0/ruby-nextgen/json-string-body.yaml new file mode 100644 index 000000000000..2bac06b50bc2 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/ruby-nextgen/json-string-body.yaml @@ -0,0 +1,38 @@ +openapi: 3.0.3 +info: + title: probes + version: 1.0.0 +paths: + /greeting: + get: + operationId: getGreeting + responses: + '200': + description: OK + content: + application/json: + schema: + type: string + /pets: + get: + operationId: listPets + responses: + '200': + description: OK + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Pet' +components: + schemas: + Pet: + type: object + properties: + birthday: + type: string + format: date + code: + type: string + pattern: '^[a-z]+$' diff --git a/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/configuration.rb b/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/configuration.rb index 2649242b82b2..0eb0ba838dbe 100644 --- a/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/configuration.rb +++ b/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/configuration.rb @@ -31,7 +31,6 @@ def configure_faraday(conn) conn.request :multipart conn.request :url_encoded conn.request :json - conn.response :json, content_type: /\bjson$/ conn.options.timeout = timeout conn.response :logger, logger if logger && debugging @middlewares.each { |mw, mw_args, mw_block| conn.use(mw, *mw_args, &mw_block) } diff --git a/samples/client/petstore/ruby-nextgen/lib/petstore/configuration.rb b/samples/client/petstore/ruby-nextgen/lib/petstore/configuration.rb index d741267c89af..58f0d697c90a 100644 --- a/samples/client/petstore/ruby-nextgen/lib/petstore/configuration.rb +++ b/samples/client/petstore/ruby-nextgen/lib/petstore/configuration.rb @@ -31,7 +31,6 @@ def configure_faraday(conn) conn.request :multipart conn.request :url_encoded conn.request :json - conn.response :json, content_type: /\bjson$/ conn.options.timeout = timeout conn.response :logger, logger if logger && debugging @middlewares.each { |mw, mw_args, mw_block| conn.use(mw, *mw_args, &mw_block) } diff --git a/samples/client/petstore/ruby-nextgen/spec/connection_spec.rb b/samples/client/petstore/ruby-nextgen/spec/connection_spec.rb new file mode 100644 index 000000000000..a55781346ca4 --- /dev/null +++ b/samples/client/petstore/ruby-nextgen/spec/connection_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'spec_helper' + +class StubReplyMiddleware < Faraday::Middleware + def initialize(app, body) + super(app) + @body = body + end + + def call(env) + env.status = 200 + env.body = @body + env.response_headers = Faraday::Utils::Headers.new('Content-Type' => 'application/json') + env.response = Faraday::Response.new(env) + end +end + +RSpec.describe Petstore::Connection do + def connection_replying_with(body) + Petstore::Client.new(base_url: 'http://localhost') { |c| c.use(StubReplyMiddleware, body) }.connection + end + + it 'deserializes a JSON string body to a String instead of raising ApiError' do + response = connection_replying_with('"hello"').call(:GET, '/greeting') + + expect(response.data).to eq('hello') + expect(response.status).to eq(200) + end + + it 'builds a model from a JSON object body' do + response = connection_replying_with({ 'name' => 'Rex' }.to_json) + .call(:GET, '/pet/1', type: Petstore::Models::Pet) + + expect(response.data).to be_a(Petstore::Models::Pet) + expect(response.data.name).to eq('Rex') + end + + it 'raises ApiError on a body that is not valid JSON' do + expect do + connection_replying_with('not json').call(:GET, '/greeting') + end.to raise_error(Petstore::ApiError, /invalid JSON in response body/) + end + + it 'returns nil data for an empty body' do + expect(connection_replying_with('').call(:GET, '/greeting').data).to be_nil + end +end