You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: OAPI Codegen only generates the models that are present in an API YAML file. #1354
After moving shared models into the 'Models' directory and generating APIs from folder paths, the shared models are not being utilized for generating Go structs. OAPI Codegen only generates the models that are present in an API YAML file. When a request or response utilizes a type from shared models, it generates this type in the following format:
type Address have not been generated. If i have in oapi-specification file reference to models, for example i have endpoint "Create User" and user has a field address, than user would be generated in format
type User struct {
Id uuid.UUID
Name string
Image *Image
Adress *struct {
Street string
City string
another fields...
}
}
CreatedAt time.Time
}
But in this case I cannot use Address direct as a generated type.
You can force the Address to be a full struct instead of an anonymous struct by adding x-go-type-name: Address to its yaml definition. However, I came across this issue while checking whether the subsequent issue I found after doing this had been raised or not. It only occurs with sub types of sub types though, so you should be ok.
@jamesphillpotts-fr Hi James, Thank You for your answer. In this particular case it works nice, but as you have mentioned - if there are any sub types of sub type, that they won't been generated. There is also one more thing - if sub type has a field with ENUM, than it won't be generated.
The recommended approach for shared models across multiple spec files is to either: (1) consolidate schemas into a single spec, (2) use x-go-type-name extensions on referenced schemas (fixed for sub-types in #1397), or (3) use a separate models generation pass with the full spec. Since #1397 was merged, x-go-type-name now works for nested sub-types as well. Awaiting reply since May 2024 — please reopen if the issue persists with the latest version.
Activity
jamietanna commentedon Nov 14, 2023
Hey, would you mind providing more details? Preferably a minimal reproduction, with a specification, and the version of oapi-codegen you're using.
driif commentedon Nov 14, 2023
github.com/deepmap/oapi-codegen v1.12.3
I have folder
api
By generating types, only types have been generated, that have been described in oapi-specifications.
Example of models
type Address have not been generated. If i have in oapi-specification file reference to models, for example i have endpoint "Create User" and user has a field address, than user would be generated in format
jamietanna commentedon Nov 17, 2023
Can you please try and validate with v2? There have been quite a few changes since v1.12 that may have resolved this
driif commentedon Nov 22, 2023
Hi @jamietanna , I have tried it also with v2 Version, but unfortunately shared Models haven't been generated.
markuswustenberg commentedon Dec 6, 2023
@driif is it maybe because your unused models have been pruned? Have you tried this setting in your config?
driif commentedon Dec 6, 2023
@markuswustenberg Thank You for your reply, I have tried it, but unfortunately it also doesn't work in this case.
jamesphillpotts-fr commentedon Dec 19, 2023
You can force the
Addressto be a full struct instead of an anonymous struct by addingx-go-type-name: Addressto its yaml definition. However, I came across this issue while checking whether the subsequent issue I found after doing this had been raised or not. It only occurs with sub types of sub types though, so you should be ok.driif commentedon Dec 19, 2023
@jamesphillpotts-fr Hi James, Thank You for your answer. In this particular case it works nice, but as you have mentioned - if there are any sub types of sub type, that they won't been generated. There is also one more thing - if sub type has a field with ENUM, than it won't be generated.
jamesphillpotts-fr commentedon Dec 20, 2023
Yep, see #1397 :-)
jamietanna commentedon May 18, 2024
@driif mind seeing if the changes suggested above by @jamesphillpotts-fr works for you, alongside the latest changes on
master/ from #1397 ?mromaszewicz commentedon Feb 7, 2026
The recommended approach for shared models across multiple spec files is to either: (1) consolidate schemas into a single spec, (2) use
x-go-type-nameextensions on referenced schemas (fixed for sub-types in #1397), or (3) use a separate models generation pass with the full spec. Since #1397 was merged,x-go-type-namenow works for nested sub-types as well. Awaiting reply since May 2024 — please reopen if the issue persists with the latest version.