Skip to content

Generating Struct with Embedded Imported Type #1622

Closed

Description

@user-llmm

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.

Activity

  1. veritymedia commented on Oct 14, 2024

    @veritymedia

    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??

  2. Jiehong commented on Nov 12, 2024

    @Jiehong

    Looks like there is a old draft PR to re-add this: #1295

  3. mromaszewicz commented on Sep 26, 2026

    @mromaszewicz
    Member

    We've revisited this with the new schema-merging-behavior: v3 (#2568). The default, v2, silently drops the x-go-type member: your MyClaims comes out with only Org and User. v3 reports it instead:

    allOf can't merge #/components/schemas/JWTStandardClaims with an inline schema with required, properties: x-go-type replaces #/components/schemas/JWTStandardClaims with jwt.StandardClaims, whose fields are unknown, so it can only be annotated (description, nullable, ...). Give the composition an x-go-type of its own
    

    An x-go-type's fields are unknown to the generator, so v3 can't merge it with other members into a new struct. An allOf of it and members that only annotate it, such as a description or nullable, is that Go type. For the struct you want, schema-merging-behavior: v1 embeds it: with your spec it generates

    type MyClaims struct {
    	Org  string `json:"org"`
    	User string `json:"user"`
    	JWTStandardClaims `yaml:",inline"`
    }

    where JWTStandardClaims is an alias of jwt.StandardClaims. Alternatively, give MyClaims an x-go-type of its own, pointing to a struct you write.

    We'd especially like your feedback here: would you rather v3 embedded an x-go-type member next to the other members' fields, as v1 does?

    To try it, set this in your configuration:

    compatibility:
      schema-merging-behavior: v3

    There's no release with v3 yet, so you'll need the latest main, for example go 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions