libopenapi icon indicating copy to clipboard operation
libopenapi copied to clipboard

Rolodex.Resolve() doesn't override the description from #ref definition

Open mlsmaycon opened this issue 1 year ago • 6 comments

Hello, it seems like the Rolodex.Resolve() method doesn't override the description from #ref definition and resolving all refs.

I have the following spec example:

openapi: 3.1.0
info:
  title: Example
  version: 0.0.1
components:
  schemas:
    OSVersionCheck:
      description: Posture check for the version of operating system
      type: object
      properties:
        android:
          description: Minimum version of Android
          $ref: '#/components/schemas/MinVersionCheck'
      example:
        android:
          min_version: "13"
    MinVersionCheck:
      description: Posture check for the version of operating system
      type: object
      properties:
        min_version:
          description: Minimum acceptable version
          type: string
          example: "14.3"
      required:
        - min_version

After outputting the result I got this:

openapi: 3.1.0
info:
    title: Example
    version: 0.0.1
components:
    schemas:
        OSVersionCheck:
            description: Posture check for the version of operating system
            type: object
            properties:
                android:
                    description: Posture check for the version of operating system
                    type: object
                    properties:
                        min_version:
                            description: Minimum acceptable version
                            type: string
                            example: "14.3"
                    required:
                        - min_version
            example:
                android:
                    min_version: "13"
        MinVersionCheck:
            description: Posture check for the version of operating system
            type: object
            properties:
                min_version:
                    description: Minimum acceptable version
                    type: string
                    example: "14.3"
            required:
                - min_version

The expected output is:

...
                android:
                    description: Minimum version of Android
                    type: object
                    properties:
                        min_version:
                            description: Minimum acceptable version
                            type: string
                            example: "14.3"
                    required:
                        - min_version
...

This is the code I am using:

package main

import (
	"io/ioutil"
	"os"

	"github.com/pb33f/libopenapi/index"
	"gopkg.in/yaml.v3"
)

func main() {

	inFile, _ := ioutil.ReadFile("in.yml")

	var rootNode yaml.Node
	_ = yaml.Unmarshal(inFile, &rootNode)

	indexConfig := index.CreateClosedAPIIndexConfig()

	rolodex := index.NewRolodex(indexConfig)

	rolodex.SetRootNode(&rootNode)

	indexedErr := rolodex.IndexTheRolodex()
	if indexedErr != nil {
		panic(indexedErr)
	}

	rolodex.Resolve()

	node := rolodex.GetRootNode()

	b, e := yaml.Marshal(node)
	if e != nil {
		panic(e)
	}

	var newNode yaml.Node
	_ = yaml.Unmarshal(b, &newNode)

	os.WriteFile("out.yml", b, 0644)

}

mlsmaycon avatar Feb 28 '24 08:02 mlsmaycon

There is a known bug in the resolver that appears haphazardly when resolving async extracted refs (which is the default).

The solution (until the root cause is fixed). Is to extract references synchronously by setting the ExtractRefsSequentially boolean to true on your indexConfig

https://github.com/pb33f/libopenapi/blob/main/index/index_model.go#L149

daveshanley avatar Feb 28 '24 10:02 daveshanley

Hi @daveshanley, thanks for responding. I've tried the configuration, but it didn't work.

mlsmaycon avatar Feb 28 '24 13:02 mlsmaycon

Oh, sorry, I mis-read your problem.

The result you have is the expected outcome of how the resolver works. It's not performing any kind of property merging or overriding, it's simply resolving the references, literally by re-pointing the underlying node tree. It's resolving like a compiler resolves pointers, it is not going to deliver what you're looking for.

This would be a new feature.

daveshanley avatar Feb 28 '24 14:02 daveshanley

Got it, thanks for the feedback.

Ok, I thought it was a bug because of the way it was defined in the 3.1 Reference Object documentation.

Do you want me to open another issue as a feature request?

mlsmaycon avatar Feb 28 '24 15:02 mlsmaycon

yes please!

daveshanley avatar Feb 28 '24 17:02 daveshanley

This is somewhat related to this issue https://github.com/pb33f/libopenapi/issues/90

TristanSpeakEasy avatar Apr 29 '24 10:04 TristanSpeakEasy