swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.89k stars 6.03k forks source link

Can only deserialize JSON with ruby-client #1303

Open Micka33 opened 8 years ago

Micka33 commented 8 years ago

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.

wing328 commented 8 years ago

@Micka33 currently the Ruby API client (and also API client in other languages) only supports deserializing JSON response into models.

For your case, do you mind sharing what the XML response looks like? and do you expect the XML response automatically desserialize into proper models or you're simply looking for the raw XML response so that you can process it further?

Micka33 commented 8 years ago

Hi @wing328. Just letting you know that I will come back to you soon, about this issue.

Shruti-R commented 8 years ago

Hi @wing328 Is xml serializer included in v2.1.5 for Ruby API client?

wing328 commented 8 years ago

@ShrutiRuparel Ruby API client includes JSON serializer but not XML serializer but we welcome contribution to add the support for XML serializer for Ruby API client.

Please let us know if you've cycle to contribute and I'll show you where to start.