swagger-api / swagger-api/swagger-codegen

Can only deserialize JSON with ruby-client

Open
#1,303 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Ruby Enhancement: Feature General: Awaiting feedback
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

I expect the returned value to be an XML object. however I can't achieve this because the accepted returned values are only JSON.
I am not clear so here the concerned code :

https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache#L91

    # Deserialize the response to the given return type.
    #
    # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
    def deserialize(response, return_type)
      body = response.body
      return nil if body.nil? || body.empty?

      # handle file downloading - save response body into a tmp file and return the File instance
      return download_file(response) if return_type == 'File'

      # ensuring a default content type
      content_type = response.headers['Content-Type'] || 'application/json'

      unless content_type.start_with?('application/json')
        fail "Content-Type is not supported: #{content_type}"
      end

      begin
        data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
      rescue JSON::ParserError => e
        if %w(String Date DateTime).include?(return_type)
          data = body
        else
          raise e
        end
      end

      convert_to_type data, return_type
    end

This means that, (AFAIK) with the ruby client, I can only request an API that returns JSON.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/swagger-codegen/src/main/resources/ruby/api_client.mustache at the deserialize method shown in the issue. Trace how response content types and returned values are handled, then verify the Ruby client can process an XML response without rejecting its Content-Type; no test file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.