Repository navigation
Description
Hi all, I am attempting to generate a struct type that includes an embedded field from an imported type. However, I am not achieving the desired result.
Here is the struct I aim to generate:
import "github.com/golang-jwt/jwt"
type MyClaims struct {
Org string `json:"org"`
User string `json:"user"`
jwt.StandardClaims
}The spec I am using is as follows:
MyClaims:
allOf:
- properties:
user:
type: string
org:
type: string
required:
- user
- org
- $ref: '#/components/schemas/JWTStandardClaims'
JWTStandardClaims:
type: object
x-go-type: jwt.StandardClaims
x-go-type-import:
path: github.com/golang-jwt/jwt
name: jwtold-merge-schemas: false
When I use the default merge schemas configuration (old-merge-schemas: false), the generated MyClaims is merely a type definition on jwt.StandardClaims, and the additional fields are not included:
import jwt "github.com/golang-jwt/jwt"
// MyClaims defines model for MyClaims.
type MyClaims jwt.StandardClaims
// JWTStandardClaims defines model for JWTStandardClaims.
type JWTStandardClaims jwt.StandardClaimsold-merge-schemas: true
If I switch to the old merge schemas configuration (old-merge-schemas: true), I get a struct with the JWTStandardClaims type embedded, rather than jwt.StandardClaims. The generated struct includes all the fields, but the embedded struct is not of the expected type:
import jwt "github.com/golang-jwt/jwt"
// MyClaims defines model for MyClaims.
type MyClaims struct {
// Embedded fields due to inline allOf schema
Org string `json:"org"`
User string `json:"user"`
// Embedded struct due to allOf(#/components/schemas/JWTStandardClaims)
JWTStandardClaims `yaml:",inline"`
}
// JWTStandardClaims defines model for JWTStandardClaims.
type JWTStandardClaims jwt.StandardClaimsIs there a configuration or method that would allow me to generate the desired struct? Any guidance would be greatly appreciated.
Activity
veritymedia commentedon Oct 14, 2024
I would love to also get some clarity on this. I have spent a good 12 hours trying to generate oapi spec from imported types. Up to the point of writing a module with AST parsing to detect structs and myself generate the schema...
Is x-go-type/-import supposed to be for this??
Jiehong commentedon Nov 12, 2024
Looks like there is a old draft PR to re-add this: #1295
mromaszewicz commentedon Sep 26, 2026
We've revisited this with the new
schema-merging-behavior: v3(#2568). The default, v2, silently drops thex-go-typemember: yourMyClaimscomes out with onlyOrgandUser. v3 reports it instead:An
x-go-type's fields are unknown to the generator, so v3 can't merge it with other members into a new struct. AnallOfof it and members that only annotate it, such as a description ornullable, is that Go type. For the struct you want,schema-merging-behavior: v1embeds it: with your spec it generateswhere
JWTStandardClaimsis an alias ofjwt.StandardClaims. Alternatively, giveMyClaimsanx-go-typeof its own, pointing to a struct you write.We'd especially like your feedback here: would you rather v3 embedded an
x-go-typemember next to the other members' fields, as v1 does?To try it, set this in your configuration:
There's no release with v3 yet, so you'll need the latest
main, for examplego run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@main --config=config.yaml spec.yaml. docs/schema-merging.md describes how v3 differs from v2. v3 is an option for now, and we plan to make it the default once it has been tested for a while, so we're eager to hear your feedback: whether it works with your specs, and anything it generates that you didn't expect.This message was generated by AI.
feat(v3): allOf can only annotate external and x-go-type schemas (oap…