pb33f/libopenapi

schema build fails when file references contain references to other files

Domarx opened this issue · 2 comments

Domarx commented

Consider this exploded schema:

# openapi.yaml
openapi: 3.1.0
info:
  title: Example
  version: 0.1.0
components:
  responses:
    GetBox:
      $ref: 'responses/get_box.yaml'
  schemas:
    item:
      $ref: 'schemas/item.yaml'
    box:
      $ref: 'schemas/box.yaml'
---
# responses/get_box.yaml
description: Gets the box
content:
  application/json:
    schema:
      $ref: '../schemas/box.yaml'
---
# schemas/item.yaml
type: object
title: Some generic data
properties:
  name:
    type: string
---
# schemas/box.yaml
type: object
title: Object used to store items
properties:
  items:
    type: array
    items:
      $ref: 'item.yaml'

Code to run this:

func main() {
	// openapi.Spec is embedded openapi.yaml
	doc, err := libopenapi.NewDocumentWithConfiguration(openapi.Spec, &datamodel.DocumentConfiguration{
		BasePath:                "./openapi",
		AllowFileReferences:     true,
		AllowRemoteReferences:   true,
		ExtractRefsSequentially: true,
	})
	if err != nil {
		panic(err)
	}

	_, errs := doc.BuildV3Model()
	if err := errors.Join(errs...); err != nil {
		panic(err)
	}
}

It results in:

panic: schema build failed: reference '../schemas/box.yaml' cannot be found at line 5, col 13

If I try to build the doc without the response components, it succeeds and I get this schema:

openapi: 3.1.0
info:
  title: Example
  version: 0.1.0
components:
  schemas:
    item:
      type: object
      title: Some generic data
      properties:
        name:
          type: string
    box:
      type: object
      title: Object used to store items
      properties:
        items:
          type: array
          items:
            type: object
            title: Some generic data
            properties:
              name:
                type: string

so file references from the same folder seem to work fine.

This is a bug with the model builder, this should work.

This has been fixed in v0.15.5 This was a genuine gap in the code when building the model.