Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby client nullable #4391

Merged
merged 3 commits into from
Nov 7, 2019
Merged

Conversation

slemrmartin
Copy link
Contributor

@slemrmartin slemrmartin commented Nov 6, 2019

This PR fixes Ruby clients on columns with "nullable": true. Attribute's key with nil value isn't skipped, but sent to server with nil value.

Issue: #4363 (comment)

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

@slemrmartin
Copy link
Contributor Author

@cliffano @zlx @autopp please review

@slemrmartin
Copy link
Contributor Author

cc @wing328

@autopp
Copy link
Contributor

autopp commented Nov 6, 2019

@slemrmartin Thanks for PR, I'll review it.

@autopp autopp self-assigned this Nov 6, 2019
Copy link
Contributor

@autopp autopp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slemrmartin This PR seems to solve original issue. However, the reverse problem appears to occur.
Even if nullable attribute is omitted, null is always sent to server.

I think we can solve both problems by using instance_variable_defined? instead of openapi_nullable.
How about that?

@slemrmartin
Copy link
Contributor Author

slemrmartin commented Nov 6, 2019

@autopp If nullable attribute is omitted, then !self.class.openapi_nullable.include?(attr) condition is true, isn't it? Which means that null isn't sent to server. Or which case I'm missing?

@autopp
Copy link
Contributor

autopp commented Nov 6, 2019

@slemrmartin Sorry for the incomprehension.
Let me illustrate my concern with the following model example.

Foo:
  type: object
  properties:
    x:
      type: integer
      nullable: true

On current master:

p OpenapiClient::Foo.new({ x: 1 }).to_hash   #=> {:x=>1}
p OpenapiClient::Foo.new({ x: nil }).to_hash #=> {} <- always empty
p OpenapiClient::Foo.new({}).to_hash         #=> {}

And on this branch:

p OpenapiClient::Foo.new({ x: 1 }).to_hash   #=> {:x=>1}
p OpenapiClient::Foo.new({ x: nil }).to_hash #=> {:x=>nil}
p OpenapiClient::Foo.new({}).to_hash         #=> {:x=>nil} <- always nil

For these, I think the following are expected:

p OpenapiClient::Foo.new({ x: 1 }).to_hash   #=> {:x=>1}
p OpenapiClient::Foo.new({ x: nil }).to_hash #=> {:x=>nil}
p OpenapiClient::Foo.new({}).to_hash         #=> {}

Please let me know if I misunderstand.

@slemrmartin
Copy link
Contributor Author

@autopp now I understand, thanks for clarification.
Then it should be combination of openapi_nullable and instance_variable_defined? I think, because if nullable is not defined or false in definition, then variable x set to nil shouldn't be allowed

    x:
      type: integer
      nullable: true
p OpenapiClient::Foo.new({ x: nil }) #=> {:x=>nil}
p OpenapiClient::Foo.new({ }) #=> {}

, but

    x:
      type: integer
      nullable: false
p OpenapiClient::Foo.new({ x: nil }) #=> {}
p OpenapiClient::Foo.new({ }) #=> {}

I don't know openapi specification to the depth so correct me if I'm wrong

[
{{#vars}}
{{#isNullable}}
:'{{{name}}}'{{#hasMore}},{{/hasMore}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent seems to be too depth.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about defining it as a hash instead similar to self.openapi_types?

If an object contains a lot of nullable properties, using include? may result in performance issues.

Copy link
Contributor Author

@slemrmartin slemrmartin Nov 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see usage for key-value, but what about using Set instead of Array, it should have better performance for include?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the format

def self.openapi_nullable
      Set.new([
      ])
end

is it possible to define it in template in a way that empty set won't generate new line?

Copy link
Contributor

@autopp autopp Nov 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... it seems to be difficult because there is no way to directly enumerate nullable attributes in template.
@wing328 Do you have any ideas?

By the way, I don't like this format, but it's acceptable (Rubocop also allows this), so I think this PR can be merged.
Isn't it okay to make another PR for reformatting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set is definitely better than array in this case.

About empty set, I think it's acceptable for day 1 requirement. We can always improve it later with another PR.

@autopp
Copy link
Contributor

autopp commented Nov 6, 2019

@slemrmartin your suggestion sounds good 👍
Can you fix it with a comment about the indentation?

@wing328 wing328 merged commit f0bcaaf into OpenAPITools:master Nov 7, 2019
@slemrmartin slemrmartin deleted the ruby-client-nullable branch November 7, 2019 08:15
@autopp autopp mentioned this pull request Nov 7, 2019
6 tasks
jimschubert added a commit to jimschubert/openapi-generator that referenced this pull request Nov 10, 2019
* master: (28 commits)
  [meta] Support Kotlin meta generator (OpenAPITools#4156)
  [Go][Server] minor enhancement to the template (OpenAPITools#4417)
  Replace the old ResourceSupport (OpenAPITools#4426)
  [Core, Rust Server, ASP.NET Core] Fix Codegen Operation Scope Consistency (OpenAPITools#3495)
  Add Go Server featureCORS option (OpenAPITools#4400)
  Fix treatment of nullable types in a few more places (OpenAPITools#4315)
  prefix local variable with localVar (OpenAPITools#4402)
  [kotlin][client] gson complete integration (OpenAPITools#4332)
  [kotlin] [bugfix] [maven-plugin]: prevent ClassCastException with boolean config options (OpenAPITools#4361)
  add sbt, bazel to integration (OpenAPITools#4416)
  Add a blog post tutorial about generating Java clients using OpenAPI v3 (OpenAPITools#4405)
  add freshcells to company list (OpenAPITools#4414)
  Update isSet when the object is received from callback. (OpenAPITools#4385)
  Ruby client nullable (OpenAPITools#4391)
  Fixes Kotlin client property names that include a dollar sign for template override (OpenAPITools#4351)
  [Python] [Performance] Avoid unnessacary checks inside the loop (OpenAPITools#4305)
  Add QEDIT as a company that's using OpenAPI Generator (OpenAPITools#4392)
  update cpp flag for pistache (OpenAPITools#4386)
  Feature optional emit default values (OpenAPITools#4347)
  skip the test as async call may have finished (OpenAPITools#4377)
  ...
@wing328
Copy link
Member

wing328 commented Nov 15, 2019

@slemrmartin thanks for the PR, which has been included in the v4.2.1 release: https://twitter.com/oas_generator/status/1195339336922759168

@slemrmartin
Copy link
Contributor Author

slemrmartin commented Nov 15, 2019

@wing328 thanks for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants