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

InternalizeRefs doesn't internalize all schemas if external schema also refers to other schema

Open kylehodgetts opened this issue 3 years ago • 1 comments

I apologise for the title gore, allow me to try and explain my issue more.

My api file refers to a response schema stored in another file ./schemas.yaml. When I load it with the loader and allow ExternalRefs and run InternalizeRefs, the resulting Components contains only the schema that I refer to directly. However JournalEntry has a local reference to another schema Record. I would expect this schema to also be in Components after

API - openapi.yaml

openapi: 3.0.0
info:
  title: foo
  version: 0.0.0
paths:
  /foo:
    get:
      responses:
        '200':
          description: Some description value text
          content:
            application/json:
              schema:
                $ref: './schema.yaml#/components/schemas/JournalEntry'

Schemas - schema.yaml

components:
  schemas:
    Account:
      required:
        - name
        - nature
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        description:
          type: string
        type:
          type: string
          enum:
            - assets
            - liabilities
        nature:
          type: string
          enum:
            - asset
            - liability
    Record:
      required:
        - account
        - concept
      type: object
      properties:
        account:
          $ref: "#/components/schemas/Account"
        concept:
          type: string
        partial:
          type: number
        credit:
          type: number
        debit:
          type: number
    JournalEntry:
      required:
        - type
        - creationDate
        - records
      type: object
      properties:
        id:
          type: string
        type:
          type: string
          enum:
            - daily
            - ingress
            - egress
        creationDate:
          type: string
          format: date
        records:
          type: array
          items:
            $ref: "#/components/schemas/Record"

Code to test

package main

import (
	"context"
	"fmt"

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

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

	doc.InternalizeRefs(context.Background(), nil)

	fmt.Println("schemas")
	for k := range doc.Components.Schemas {
		fmt.Println("\t", k)
	}
}

Output

➜ go run main.go
schemas
         JournalEntry

kylehodgetts avatar Sep 26 '22 14:09 kylehodgetts

@fenollp could you take a look when you have the time

kylehodgetts avatar Sep 29 '22 09:09 kylehodgetts