Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MediaType example incorrectly uses low-level node reference #6

Closed
danielgtaylor opened this issue Nov 4, 2022 · 2 comments
Closed
Assignees

Comments

@danielgtaylor
Copy link
Contributor

danielgtaylor commented Nov 4, 2022

I've run into a minor bug where an OpenAPI 3 MediaType example is being incorrectly loaded from the low-level model into the high-level model. It is being represented as a low-level node ref. The same example field inside the schema does work correctly.

As a temporary workaround I have done this:

for mt, item := range op.RequestBody.Content {
	// HACK: example is just a low-level node ref which may in fact just
	// be a node representing `null` or the real example.
	if n, ok := item.Example.(low.NodeReference[any]); ok {
		item.Example = n.Value
	}

Here's a simple test to show the bug in action:

$ go run ./bug
Obj {{"foo": "hello"} 0x1400011da40 0x1400011d9a0}             <---- whoops
Schema {"bar": "world"}

Code:

package main

import (
	"fmt"

	"github.com/pb33f/libopenapi"
)

var data = `
openapi: "3.0"
paths:
  /test:
    get:
      responses:
        "200":
          content:
            application/json:
              example: "{\"foo\": \"hello\"}"
              schema:
                type: object
                example: "{\"bar\": \"world\"}"
                properties:
                  foo:
                    type: string
                  bar:
                    type: string
`

func main() {
	doc, err := libopenapi.NewDocument([]byte(data))
	if err != nil {
		panic(err)
	}

	result, errs := doc.BuildV3Model()
	if len(errs) > 0 {
		panic(errs)
	}

	m := result.Model

	fmt.Println("Obj", m.Paths.PathItems["/test"].Get.Responses.Codes["200"].Content["application/json"].Example)

	fmt.Println("Schema", m.Paths.PathItems["/test"].Get.Responses.Codes["200"].Content["application/json"].Schema.Schema().Example)
}

Part of danielgtaylor/restish#115.

@daveshanley
Copy link
Member

Nice catch! I'll address this ASAP.

@daveshanley daveshanley self-assigned this Nov 7, 2022
daveshanley added a commit that referenced this issue Nov 7, 2022
The example was not being pulled through correctly, it was throwing the whole low.NodeReference in there. This has been resolved now and the value of the example is correctly pulled through to the MediaType high level object.

Signed-off-by: Dave Shanley <dave@quobix.com>
@daveshanley
Copy link
Member

This has been resolved, and the example is now correctly pulled up into the high-level model (forgot the .Value). It's fixed in v0.1.11

Thanks once again for your help to make this a better library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants