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 @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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]+$'
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def configure_faraday(conn)
conn.request :multipart
conn.request :url_encoded
conn.request :json
conn.response :json, content_type: /\bjson$/

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.

P2: Removing the response JSON middleware also changes ApiError#body for JSON error responses: it was a parsed Hash and is now the raw String. The README already describes e.body as the raw body, so this may be intended, but the PR does not call it out. Please note this as a behavior change, or add a spec covering the error body shape.

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 samples/client/petstore/ruby-nextgen/lib/petstore/configuration.rb, line 34:

<comment>Removing the response JSON middleware also changes `ApiError#body` for JSON error responses: it was a parsed Hash and is now the raw String. The README already describes `e.body` as the raw body, so this may be intended, but the PR does not call it out. Please note this as a behavior change, or add a spec covering the error body shape.</comment>

<file context>
@@ -31,7 +31,6 @@ def configure_faraday(conn)
       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) }
</file context>

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) }
Expand Down
48 changes: 48 additions & 0 deletions samples/client/petstore/ruby-nextgen/spec/connection_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading