Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive / Deep ref hanging / Possible Compatibility Issue #423

Closed
lewismacdonald opened this issue Sep 21, 2021 · 3 comments · Fixed by #432
Closed

Recursive / Deep ref hanging / Possible Compatibility Issue #423

lewismacdonald opened this issue Sep 21, 2021 · 3 comments · Fixed by #432

Comments

@lewismacdonald
Copy link

lewismacdonald commented Sep 21, 2021

I've noticed when trying to reference an external schema, the following hangs in an infinite parsing loop, perhaps because of some recursive refs in the external document:

package main

import (
	"fmt"

	"github.com/getkin/kin-openapi/openapi3"
)

var spec = `
info:
  description: test
  title: test
  version: 0.0.0
openapi: 3.0.1
paths:
  /api/bundles:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Data'
components:
  schemas:
    Data:
      description: rbac Data
      properties:
        roles:
           $ref: "https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json#/definitions/io.k8s.api.rbac.v1.RoleList"
`

func main() {
	loader := openapi3.NewLoader()
	loader.IsExternalRefsAllowed = true
	loader.LoadFromData([]byte(spec))
}

I'm aware that the referenced document is a swagger v2 document, but given that its just a schema ref to jsonschema objects, i hoped it would be possible to reference succesfully. from what i can see, incompatability doesn't seem to be the problem.

I can see that the code seems to just end up circling around loader.resolveSchemaRef for #/definitions/io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.JSONSchemaProps and a few other types in the reference document which are very recursive in their definition, but i'm unsure if the reason for hanging is an endless recursive loop, or what.

Would be happy to contribute a fix if there is one but so far i've been unable to diagnose what exactly is going on..

fenollp added a commit to fenollp/kin-openapi that referenced this issue Oct 2, 2021
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
@fenollp
Copy link
Collaborator

fenollp commented Oct 2, 2021

resolveSchemaRef depends on the input document being openapiv3. Did you try converting the swagger.json to openapiv3 and JSON-pointing to that?

@lewismacdonald
Copy link
Author

@fenollp thanks - yes converting the entire ref document to v3 does seem to fix - but unfortunately I don't own the source document. would you have any suggestion on how to reference it?

out of interest, since the content being referenced could (as far as i understand) be any valid jsonschema document, would there be anything to do to relax resolveSchemaRef assumption that it must be an openapi v3 document? .. specifically when resolving component/definitions? or would that be nonsensical?

@fenollp
Copy link
Collaborator

fenollp commented Oct 5, 2021

Short term either write your document in v2, or load the ref document then convert it then ref it and output the result and use that as your spec.

resolveSchemaRef does what it does for historical reasons only I believe (I only arrived after it was already written this way). I agree with you and I'd prefer it to just load whatever it is JSON-pointed to.
I don't have the resources to look into this but if you do the crux of the problem is in that this is expecting/setting a *openapi3.T here

if doc, err = loader.loadFromURIInternal(resolvedPath); err != nil {
(and aggregating the various components the foreign document might have).
A good enough intermediary could be to halt and error when ref-ing a v2 document instead of looping infinitely.

fenollp added a commit to fenollp/kin-openapi that referenced this issue Aug 13, 2022
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
fenollp added a commit to fenollp/kin-openapi that referenced this issue Oct 20, 2023
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
fenollp added a commit that referenced this issue Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants