Skip to content

1.12.2 x-go-type extension and ref don't work together #863

Closed

Description

@goshlanguage

I'm not sure if there is a specific reason for how it works at current, but what I would like to do is externally reference say the kubernetes API spec, but also provide the go type instead of generating it.

At current I'm referencing say v1.Taint in an object, but I also want to reference it's openapi spec so that its rendered in the swagger/openapi docs:

    KubeSpec:
      type: object
      properties:
        taints:
          $ref: "https://raw.githubusercontent.com/kubernetes/kubernetes/v1.23.9/api/openapi-spec/v3/api__v1_openapi.json#/components/schemas/io.k8s.api.core.v1.Taint"
          x-go-type: v1.Taint
          x-go-type-import:
            name: v1
            path: k8s.io/api/core/v1

At current, when $ref is present, it seems to try to render the type via the import mapping, but I'd much prefer to provide the type.

Is this possible? If not, would a PR be welcome and any ideas where to start?

Thanks so much!

Activity

  1. vennekilde commented on Aug 30, 2023

    @vennekilde

    It seems to be impossible to use any extension fields with a reference
    For example x-oapi-codegen-extra-tags together with a $ref, which would be really useful together with an ORM like bun.

    Example

    components:
      schemas:
        Parent:
          properties:
            id:
              type: integer
            child_id:
              type: integer
            child:
              x-oapi-codegen-extra-tags:
                bun: rel:has-one,join:child_id=id
              $ref: '#/components/schemas/Child'
        Child:
          properties:
            id:
              type: integer

    Expected

    // Child defines model for Child.
    type Child struct {
    	Id *int `json:"id,omitempty"`
    }
    
    // Parent defines model for Parent.
    type Parent struct {
    	Child   *Child `bun:"rel:has-one,join:child_id=id" json:"child,omitempty"`
    	ChildId *int   `json:"child_id,omitempty"`
    	Id      *int   `json:"id,omitempty"`
    }

    Actual

    // Child defines model for Child.
    type Child struct {
    	Id *int `json:"id,omitempty"`
    }
    
    // Parent defines model for Parent.
    type Parent struct {
    	Child   *Child `json:"child,omitempty"`
    	ChildId *int   `json:"child_id,omitempty"`
    	Id      *int   `json:"id,omitempty"`
    }
  2. vennekilde commented on Aug 31, 2023

    @vennekilde

    It seems support for sibling properties with $ref got added with openapi 3.1, which updated to JSON schema 2020-12, so i assume this won't get fixed until #373 is resolved.

  3. tigerinus commented on Mar 13, 2024

    @tigerinus

    as a workaround:

            some_property:
              allOf:
                - $ref: "#/components/schemas/some_schema"
              x-go-type: SomeGoType
  4. jamietanna commented on Apr 20, 2024

    @jamietanna
    Member

    Apologies for the delay on getting back to you on this! Correct, this is due to $refs not being allowed siblings.

    Interestingly, vacuum shows that even with an OpenAPI 3.1 spec:

    # via https://github.com/deepmap/oapi-codegen/issues/863#issuecomment-1699344896
    openapi: 3.1.0
    components:
      schemas:
        Parent:
          properties:
            id:
              type: integer
            child_id:
              type: integer
            child:
              x-oapi-codegen-extra-tags:
                bun: rel:has-one,join:child_id=id
              $ref: '#/components/schemas/Child'
        Child:
          properties:
            id:
              type: integer

    This is still deemed invalid:

    spec.yaml:13:11 | error    | a `$ref` cannot be placed next to any other properties                   | no-$ref-siblings          | Schemas      | $.components.schemas.Child
    
  5. jamietanna commented on Jan 28, 2025

    @jamietanna
    Member

    THanks @paulmach for the PR! Just taking a look now

    Re-reading Vacuum's finding: https://quobix.com/vacuum/rules/schemas/no-ref-siblings/

    So it looks like OpenAPI 3.1 should no longer have this issue (but it does seem to in the example tested above)

  6. jamietanna commented on Jan 28, 2025

    @jamietanna
    Member

    as a workaround:

        some_property:
          allOf:
            - $ref: "#/components/schemas/some_schema"
          x-go-type: SomeGoType
    

    It's also possible to wrap it all in an allOf:

         some_property:
           allOf:
             - $ref: "#/components/schemas/some_schema"
             - x-go-type: SomeGoType
    
  7. jamietanna commented on Jan 28, 2025

    @jamietanna
    Member

    cc @daveshanley sorry for the ping, but do you have any thoughts here as you've likely been thinking about this a bit more than me recently?

    I'm considering that the way oapi-codegen (and tools generally should work) is that:

    • When using OpenAPI 3.0, $refs should not be allowed with siblings
      • And instead using the allOf workaround is best
    • When using OpenAPI 3.1 $refs can be used with siblings

    However, this doesn't quite match up with how Vacuum (v0.12.0) reports that this is an ERROR in both 3.0 and 3.1 - do you think that we should never allow siblings with $refs?

    Related: https://quobix.com/vacuum/rules/schemas/no-ref-siblings/

  8. Xentox-Phil commented on Mar 5, 2025

    @Xentox-Phil

    @jamietanna
    i personally think it should be possible with oapi-codegen to define additional tags with the ref because there are certain use cases as @vennekilde pointed out. Loading the spec sheet does not give me any errors with things besides the refs maybe i am missing here something but using allOf as workaround feels like a hack to me!

  9. daveshanley commented on Mar 5, 2025

    @daveshanley

    cc @daveshanley sorry for the ping, but do you have any thoughts here as you've likely been thinking about this a bit more than me recently?

    I'm considering that the way oapi-codegen (and tools generally should work) is that:

    • When using OpenAPI 3.0, $refs should not be allowed with siblings

      • And instead using the allOf workaround is best
    • When using OpenAPI 3.1 $refs can be used with siblings

    However, this doesn't quite match up with how Vacuum (v0.12.0) reports that this is an ERROR in both 3.0 and 3.1 - do you think that we should never allow siblings with $refs?

    Related: https://quobix.com/vacuum/rules/schemas/no-ref-siblings/

    Hi Jamie,

    vacuum was updated to allow approved siblings in 3.1. The latest version is v0.16.3

    https://github.com/daveshanley/vacuum/blob/main/functions/openapi/oas_no_ref_siblings.go

    You can see that 'summary' and 'description' are allowed siblings, but everything else is still a no-go. including extensions.

  10. mromaszewicz commented on Apr 25, 2026

    @mromaszewicz
    Member

    This was fixed in #1880

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions