Skip to content

Bundling with JSON components no longer works #562

@FollowTheProcess

Description

@FollowTheProcess

Issue

BundleBytesComposed no longer hoists JSON $ref targets into components/schemas. It looks like it's started from 0.33.1 onwards (I noticed test failures when doing a bump and dug into it)

Repro

Tiny openapi spec with a User schema, the Go code simply prints the bundled spec.

Try swapping $ref: "User.json" for $ref: "User.yaml" and you should see the yaml one get correctly hoisted into components, while the JSON one does not.

main.go

package main

import (
	"fmt"
	"os"

	"github.com/pb33f/libopenapi/bundler"
	"github.com/pb33f/libopenapi/datamodel"
)

func main() {
	specBytes, err := os.ReadFile("openapi.yaml")
	if err != nil {
		panic(err)
	}

	out, err := bundler.BundleBytesComposed(specBytes, &datamodel.DocumentConfiguration{
		AllowFileReferences: true,
		BasePath:            ".",
		SpecFilePath:        "openapi.yaml",
	}, &bundler.BundleCompositionConfig{
		StrictValidation: true,
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(string(out))
}

openapi.yaml

openapi: 3.0.3
info:
  title: t
  version: '1'
paths:
  /x:
    get:
      responses:
        '200':
          description: ok
          content:
            application/json:
              schema:
                $ref: "User.json" # <- Swap this for User.yaml and magically components/schemas starts to work

User.json

{
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    }
  }
}

User.yaml

type: object
properties:
  id:
    type: string

Digging

I think it has something to do with this bit:

// Skip quoted keys - they should not be treated as OpenAPI keywords
if keyNode.Style == yaml.SingleQuotedStyle || keyNode.Style == yaml.DoubleQuotedStyle {
continue

My guess is that JSON keys (because they are obviously quoted) get parsed here as yaml.DoubleQuotedStyle and are therefore skipped?

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