Allow overriding primitive types with custom types #428
+271
−211
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.
Short Description
Allow primitive types (ints, floats, string types) to be replaced with an option,
primitive-mapping
, in the YAML configuration.Motivation
Sometimes it is desirable to use a custom wrappers or different types entirely for the hard-coded types generated in
oapi-codegen
.One of such reasons is for tighter controls over JSON marshalling (e.g. check for missing fields or perform some custom validation).
This can already be done with
x-go-type
extension tag, but it is very painful to do for e.g. every integer parameter in the whole YAML spec.What has changed?
This is not a breaking change, no code generation change is made if there is no configuration change.
*Options
object as an additional parameter.primitive-mapping
, which maps the primitive type to the actual type (int32
=>null.Int32
for example).Note that
primitive-mapping
is only available in the YAML configuration at the moment (no flags). I am not sure if it is desirable to add the flags / what is the best way to do it, so I left it out.