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

Support defaulting query parameter values when not supplied, and default is set. #1703

Open
cdelst opened this issue Jul 17, 2024 · 1 comment · May be fixed by #1730
Open

Support defaulting query parameter values when not supplied, and default is set. #1703

cdelst opened this issue Jul 17, 2024 · 1 comment · May be fixed by #1730

Comments

@cdelst
Copy link

cdelst commented Jul 17, 2024

Right now, using the default parameter in openAPI specs does functionally nothing in the generator.

We could improve the strict generator by setting the query parameters to the default values set, and not have a pointer to them in the code, marking them as optional.

@percivalalb
Copy link

An up vote for addtional support for default values from me. I came across this when I had a query param that had a default value of false, found myself having check if it was nil and assuming a default which introduces a disconnect between the OAPI spec and the code:

var field bool
if params.Field != nil && params.Field {
    field = params.Field
}

// do something with `field`

I think the auto-generated client code requires no changes since a pointer value (!= nil) is used to differentate between a user provided value and no value.

So as you suggest, if the schema has a default value then having that set automatically if the request does not give a value would be a start. Dropping the pointer for a more user-friendly experience would be the next step. In my example I could then just use params.Field directly.

I've been having a look at the code changes to support this, I think that passing the default value to https://github.com/oapi-codegen/runtime/blob/912bc9f39f1e8fd175f1f39c48b4adf5ceca39f1/bindparam.go#L309 and using it is the majority of the solution to respecting the default value.

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 a pull request may close this issue.

2 participants