Skip to content

[BUG][Ruby] build_from_hash method doesn't consider nullable #6780

@asatobi

Description

@asatobi

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

As I wrote in the title, build_from_hash method doesn't consider nullable attribute.
I implemented an API to create object and return that object after successfully created.
I've set nullable:true to end_year attribute so that I can request and create object with null value. However when client model makes response body, the model removes the attribute with null value.
So response returns object without attribute with null value.
Here is my model file generated by openapi-generator↓

# Builds the object from hash
# @param [Hash] attributes Model attributes in the form of hash
# @return [Object] Returns the model itself
def build_from_hash(attributes)
  return nil unless attributes.is_a?(Hash)
  self.class.openapi_types.each_pair do |key, type|
    if type =~ /\AArray<(.*)>/i
      # check to ensure the input is an array given that the attribute
      # is documented as an array but the input is not
      if attributes[self.class.attribute_map[key]].is_a?(Array)
        self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
      end
    # Because of here,  @end_year won't be added when end_year is nil
    elsif !attributes[self.class.attribute_map[key]].nil? 
      self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
    end # or else data not found in attributes(hash), not an issue as the data can be optional
  end

  self
end
openapi-generator version

5.0.0-SNAPSHOT

OpenAPI declaration file content or url
 /schools:
    post:
      summary: create schools 
      description: create schools
      tags:
        - School
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/School'
        required: true
      responses:
        '201':
          description: Created successfuly
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/School'
components:
  schemas:
    School:
      type: object
      description: School
      required:
        - name
        - start_year
      properties:
        id:
          type: integer
          readOnly: true
        name:
          type: string
        start_year:
          type: integer
        end_year:
          type: integer
          nullable: true
        created_at:
          type: string
          format: date-time
          readOnly: true
       updated_at:
         type: string
         format: date-time
         readOnly: true
Steps to reproduce

When request body is like below..

{
    "name": "Ruby School",
    "start_year": 2019,
    "end_year": null
}

Actual output

{
    "id": 1,
    "name": "Ruby School",
    "start_year": 2019,
    "created_at": "2020-06-25T04:33:20.223+00:00",
    "updated_at": "2020-06-25T04:33:20.223+00:00"
}

Expected output (display end_year too)

{
    "id": 1,
    "name": "Ruby School",
    "start_year": 2019,
    "end_year": null,
    "created_at": "2020-06-25T04:33:20.223+00:00",
    "updated_at": "2020-06-25T04:33:20.223+00:00"
}
Command line used for generation

Ruby

#!/bin/sh
openapi_spec=https://pages.github/xxxxx/openapi.yaml
generator=ruby
output_dir=ruby
config_file=config.json

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i $openapi_spec -g $generator -o /local/$output_dir -c /local/$config_file
Related issues/PRs
Suggest a fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions