Skip to content

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

@kylehodgetts

Description

@kylehodgetts

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions