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

Deep merge params for definition #688

Merged
merged 1 commit into from
Jul 24, 2018

Conversation

jdmurphy
Copy link
Contributor

@jdmurphy jdmurphy commented Jul 23, 2018

I found that when using body params in PUT and POST, a nested entity in the params that has a description set at the entity level will not have that description in the OpenAPI spec output. For example given:

params do
  requires :spline, description: 'It is a spline', documentation: {param_type: 'body'}, type: Hash do
    requires :x, type: Numeric
    requires :y, type: Numeric
  end
  optional :reticulated, type: Boolean, default: true, desc: 'True if the spline is reticulated.'
end

a definition will be created (notice that the spline hash under properties has no description):

"postSplines":{  
   "type":"object",
   "properties":{  
      "spline":{  
         "type":"object",
         "properties":{  
            "x":{  
               "type":"integer",
               "format":"int64"
            },
            "y":{  
               "type":"integer",
               "format":"int64"
            }
         },
         "required":[  
            "x",
            "y"
         ]
      },
      "reticulated":{  
         "type":"boolean",
         "description":"True if the spline is reticulated."
      }
   },
   "required":[  
      "spline"
   ],
   "description":"Create a spline."
 }

With my fix a deep merge occurs which maintains elements on the parent object when merging a nested element: (and the description of "It is a spline" is kept)

"postSplines":{  
   "type":"object",
   "properties":{  
      "spline":{  
         "type":"object",
         "description":"It is a spline",
         "properties":{  
            "x":{  
               "type":"integer",
               "format":"int64"
            },
            "y":{  
               "type":"integer",
               "format":"int64"
            }
         },
         "required":[  
            "x",
            "y"
         ]
      },
      "reticulated":{  
         "type":"boolean",
         "description":"True if the spline is reticulated."
      }
   },
   "required":[  
      "spline"
   ],
   "description":"Create a spline."
}

while looking into this I noticed that the recursive_call function was using the property parameter as a key without regard to its format which could lead to having string and symbols mixed together specifically when nested params are being evaluated (the nested param would be the string whereas every other element would be a symbol), so I added a call to .to_sym to ensure use of symbols throughout since we are using Hash and not HashWithIndifferentAccess.

@jdmurphy jdmurphy force-pushed the deep_merge_params branch from 609698b to 7f86525 Compare July 23, 2018 19:22
@coveralls
Copy link

coveralls commented Jul 23, 2018

Coverage Status

Coverage increased (+0.0006%) to 99.529% when pulling 7f86525 on jdmurphy:deep_merge_params into c25fe68 on ruby-grape:master.

@LeFnord
Copy link
Member

LeFnord commented Jul 24, 2018

thanks @jdmurphy

@LeFnord LeFnord merged commit baa8614 into ruby-grape:master Jul 24, 2018
LeFnord pushed a commit to LeFnord/grape-swagger that referenced this pull request Feb 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants