Skip to content

Generating Struct with Embedded Imported Type #1622

@lorena-mv

Description

@lorena-mv

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions