OpenAPITools / OpenAPITools/openapi-generator

[BUG][RUBY and others] Default parameters should not be added in code generation

Open
#17,257 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Reading about parameters, the default is:

The default value is the one that the server uses if the client does not supply the parameter value in the request

As I understand, this means that default does not have to be added by the client. However, code generation adds the default value to the request. I would expect that this is not added by the client. I believe this is not specific to Ruby (the one I tried this with)

openapi-generator version

openapi-generator-cli 7.2.0-SNAPSHOT
commit : 46dd875

OpenAPI declaration file content or url
{
  "openapi": "3.0.3",
  "info": {
    "title": "Sample API",
    "version": "1.0.0"
  },
  "paths": {},
  "components": {
    "schemas": {
      "ExampleObject": {
        "type": "object",
        "properties": {
          "state": {
            "type": "string",
            "enum": [
              "foo",
              "bar"
            ],
            "default": "foo",
            "description": "The state of the object."
} } } } } }
Generation Details
❯ mkdir config
❯ touch config/dummy.json
# paste the definition into dummy.json
❯ docker run --rm \
                                             -v "$(PWD)/config/:/local" \
                                             -v "$(PWD):/out" \
                                            --pull always \
                                             openapitools/openapi-generator-cli generate \
                                               --skip-validate-spec \
                                               --generator-name ruby --output /out \
                                               --input-spec /local/dummy.json \
                                               --additional-properties gemName=dummy
Steps to reproduce
# lib/probely/models/example_object.rb

      if attributes.key?(:'state') 
        self.state = attributes[:'state']
      else
        self.state = 'foo' # Line 86
      end

I think this else should not be there

Related issues/PRs

There was this old discussion here

Suggest a fix

In the description

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 the reproduced output in lib/probely/models/example_object.rb and rerun the documented Docker command using the Ruby generator and dummy.json. Compare the generated handling of the state default with the issue's expected behavior; done means the client does not add the schema default when the parameter was not supplied, consistently across the affected generators.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, ruby
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.