Allow overriding primitive types with custom types#428
Closed
natsukagami wants to merge 4 commits intooapi-codegen:mainfrom
Closed
Allow overriding primitive types with custom types#428natsukagami wants to merge 4 commits intooapi-codegen:mainfrom
natsukagami wants to merge 4 commits intooapi-codegen:mainfrom
Conversation
This opens up schema generation in `schema.go` based on passed-in options.
Member
|
@deepmap-marcinr is this one we can take inspiration from / will be replaced by your work in the v2 side of things? |
Member
|
Apologies, meant to close #458 🫣 |
danicc097
pushed a commit
to danicc097/oapi-codegen
that referenced
this pull request
Aug 31, 2024
As noted in oapi-codegen#428, there are some cases where it can be useful to set the field ordering. This adds support for the `x-order` extension used by other OpenAPI parsers and generators, and ensures that we return keys sorted by that order, if present. This also adds a relevant test case + unit tests to validate functionality. Closes oapi-codegen#428. Co-authored-by: Filinto Duran <filinto@diagrid.io> Signed-off-by: Filinto Duran <filinto@diagrid.io> Co-authored-by: Andrew Rusakow <psihatter@gmail.com> Co-authored-by: Jamie Tanna <jamie@jamietanna.co.uk>
mromaszewicz
added a commit
to mromaszewicz/oapi-codegen
that referenced
this pull request
Feb 14, 2026
Supersedes oapi-codegen#428, since the original author's branch is gone and his PR only exists in the ref log. Added a TypeMapping configuration that lets users override the default OpenAPI type/format to Go type mappings. User-specified mappings are merged on top of built-in defaults, so only overrides need to be specified. For example, mapping string/date-time to a civil.DateTime or changing the default integer type to int64. Changes: - New TypeMapping, FormatMapping, SimpleTypeSpec types with Merge() and Resolve() methods (pkg/codegen/typemapping.go) - TypeMapping field added to Configuration struct - Merged type mapping stored on globalState, initialized in Generate() - Replaced hardcoded switch statements in oapiSchemaToGoType() with map-based lookups via globalState.typeMapping - Updated configuration-schema.json with type-mapping property and reusable $defs for format-mapping and simple-type-spec - Unit tests for merge, resolve, and default mapping completeness Co-Authored-By: natsukagami <natsukagami@gmail.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
|
Thank you for doing this @natsukagami . I tried to fix up this commit to be compatible with the current code, which has diverged greatly since when you originally submitted it, but I can't update your PR since the linked branch now contains many other changes, and i don't want to break your work. I've made a new PR, which does the same thing but is implemented differently as #2223 and made you a co-author. |
Contributor
Author
|
No worries! It's been a long time since my PR and I don't personally use oapi-codegen anymore, so it's great to have someone take over the changes! |
mromaszewicz
added a commit
to mromaszewicz/oapi-codegen
that referenced
this pull request
Feb 15, 2026
Supersedes oapi-codegen#428, since the original author's branch is gone and his PR only exists in the ref log. Added a TypeMapping configuration that lets users override the default OpenAPI type/format to Go type mappings. User-specified mappings are merged on top of built-in defaults, so only overrides need to be specified. For example, mapping string/date-time to a civil.DateTime or changing the default integer type to int64. Changes: - New TypeMapping, FormatMapping, SimpleTypeSpec types with Merge() and Resolve() methods (pkg/codegen/typemapping.go) - TypeMapping field added to Configuration struct - Merged type mapping stored on globalState, initialized in Generate() - Replaced hardcoded switch statements in oapiSchemaToGoType() with map-based lookups via globalState.typeMapping - Updated configuration-schema.json with type-mapping property and reusable $defs for format-mapping and simple-type-spec - Unit tests for merge, resolve, and default mapping completeness Co-Authored-By: natsukagami <natsukagami@gmail.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
mromaszewicz
added a commit
that referenced
this pull request
Feb 16, 2026
* feat: add configurable type mapping for OpenAPI primitive types Supersedes #428, since the original author's branch is gone and his PR only exists in the ref log. Added a TypeMapping configuration that lets users override the default OpenAPI type/format to Go type mappings. User-specified mappings are merged on top of built-in defaults, so only overrides need to be specified. For example, mapping string/date-time to a civil.DateTime or changing the default integer type to int64. Changes: - New TypeMapping, FormatMapping, SimpleTypeSpec types with Merge() and Resolve() methods (pkg/codegen/typemapping.go) - TypeMapping field added to Configuration struct - Merged type mapping stored on globalState, initialized in Generate() - Replaced hardcoded switch statements in oapiSchemaToGoType() with map-based lookups via globalState.typeMapping - Updated configuration-schema.json with type-mapping property and reusable $defs for format-mapping and simple-type-spec - Unit tests for merge, resolve, and default mapping completeness Co-Authored-By: natsukagami <natsukagami@gmail.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Move TypeMapping to OutputOptions * Add example for type-mapping * Address PR feedback Move type-mapping examples to output-options --------- Co-authored-by: natsukagami <natsukagami@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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-typeextension 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.
*Optionsobject as an additional parameter.primitive-mapping, which maps the primitive type to the actual type (int32=>null.Int32for example).Note that
primitive-mappingis 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.