Add options to OpenApiSpex.schema/1
#312
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduce options in
OpenApiSpex.schema/2
:Example
:struct?
When false,
:struct?
prevents a struct definition for the schema module. This is useful forPATCH
endpoints where the caller wants to provide a subset of the possible request params accepted by the endpoint. When the params are casted to a struct, it appears from the endpoint's perspective that the client provided all the request fields when it may have only provided a subset.This causes certain bugs when trying to update a resource, as seen on some projects that use Ecto changesets. Any param that was not passed in the update will be updated to
nil
or whatever the default value is defined for the corresponding schema property.In this example, we only want to update a user's email, but we end up updating their name to
nil
::derive?
This option resolves the issue described in #310. As stated in that issue, it is not clear what the value of defining
@derive
is, and it does present problems as described in the issue.Testing
My project has over 60 schema modules defined. After adding
struct?: false, derive?: false
to all of them, there were a few test failures due to callingMap.from_struct/1
unnecessarily. Once that call was removed, everything worked perfectly.