Generating Struct with Embedded Imported Type
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: jwt
old-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.StandardClaims
old-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.StandardClaims
Is there a configuration or method that would allow me to generate the desired struct? Any guidance would be greatly appreciated.
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??
Looks like there is a old draft PR to re-add this: https://github.com/oapi-codegen/oapi-codegen/pull/1295