kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

External file references aren't allowed

Open hbielenia opened this issue 3 years ago • 6 comments

I'm trying to parse an OpenAPI 3.0.3 document that exists on my local file system, divided into several files. I've narrowed the behaviour to following test case.

Files (all exist in the same directory, at it's root):

main.yaml

openapi: 3.0.3
servers:
  - url: http://127.0.0.1
info:
  title: kin-openapi-test
  description: kin-openapi reference case
  version: 1.0.0
paths:
  "/test":
    $ref: test.yaml
components: {}

test.yaml

---
get:
  operationId: getOk
  summary: "Just a simple operation endpoint."
  responses:
    "200":
      description: OK
      content:
        application/json:
          schema:
            type: array
            items:
              type: string

main.go

package main

import (
	"fmt"
	"github.com/getkin/kin-openapi/openapi3"
)

func main() {
	_, err := openapi3.NewLoader().LoadFromFile("main.yaml")
	if err != nil {
		fmt.Println(err.Error())
	}
}

.redocly.yaml

lint:
  extends:
    - minimal

Upon executing go run ., I'm getting the following error:

encountered disallowed external reference: "test.yaml"

The document(s) pass validation by redocly/openapi-cli using command docker run --rm -v $PWD:/spec redocly/openapi-cli lint main.yaml. This kind of file reference also seems valid in OpenAPI 3, according to https://swagger.io/docs/specification/using-ref/ .

I looked at other issues describing issues with references, but didn't found anything that could be applied to my case.

hbielenia avatar Jul 07 '22 07:07 hbielenia

The loader object has a flag "IsExternalRefsAllowed".

loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
_, err := loader.LoadFromFile("main.yaml")
if err != nil {
	fmt.Println(err.Error())
}

YoSev avatar Jul 15 '22 09:07 YoSev

I am not sure it loads external refs components well. On my example, the object MyParam does not appear on the components schemas.

main.yml

openapi: 3.0.3
servers:
  - url: http://127.0.0.1
info:
  title: kin-openapi-test
  description: kin-openapi reference case
  version: 1.0.0
paths:
  /test:
    get:
      summary: get test
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "schemas.yaml#/components/schemas/MyParam"
        "400":
          description: Not OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/MyError"

components:
  schemas:
    MyError:
      type: object
      properties:
        message:
          type: string

schemas.yml

components:
  schemas:
    MyParam:
      type: object
      properties:
        name:
          type: string
        admin:
          type: boolean

main.go

package main

import (
	"fmt"

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

func main() {
	loader := openapi3.NewLoader()
	loader.IsExternalRefsAllowed = true
	doc, err := loader.LoadFromFile("main.yaml")
	if err != nil {
		fmt.Println(err.Error())
	}

	fmt.Println("schemas")
	for k := range doc.Components.Schemas {
		fmt.Println("\t", k) // I expected Error and MyParam, but only MyError
	}

	fmt.Println("\npath")
	for k, v := range doc.Paths {
		fmt.Println("  ", k, "responses parameters:")

		for _, y := range v.Get.Responses {
			fmt.Println("    refs: ", y.Value.Content["application/json"].Schema.Ref)
			for a, b := range y.Value.Content["application/json"].Schema.Value.Properties {
				fmt.Println("      ", a, b.Value.Type)
			}
		}
	}
}

output - I expected to see both MyError and MyParam on the schemas section

schemas
         MyError

path
   /test responses parameters:
    refs:  schemas.yaml#/components/schemas/MyParam
       admin boolean
       name string
    refs:  #/components/schemas/MyError
       message string

kahlys avatar Aug 04 '22 12:08 kahlys

I haven't run your code (BTW you can always open a PR to show how code runs) but don't you have a typo here:

components:
  schemas:
    Param: # not "MyParam"
      type: object
      properties:
        name:
          type: string
        admin:
          type: boolean

Please re-open if I missed something.

fenollp avatar Aug 29 '22 14:08 fenollp

It was just a error of mine when I copy paste my files in this issue, but even with MyParam I still have the issue. (I have edited my first message)

kahlys avatar Sep 05 '22 11:09 kahlys

@fenollp You have closed the issue yourself, so I can't re-open it myself

kahlys avatar Sep 05 '22 11:09 kahlys

Ah sorry about that! Reopening.

fenollp avatar Sep 05 '22 11:09 fenollp