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

nullable reference: hard to properly identify it #283

Open
pierre-emmanuelJ opened this issue May 1, 2024 · 4 comments
Open

nullable reference: hard to properly identify it #283

pierre-emmanuelJ opened this issue May 1, 2024 · 4 comments
Labels
documentation Improvements or additions to documentation

Comments

@pierre-emmanuelJ
Copy link

pierre-emmanuelJ commented May 1, 2024

First, Thanks for your great project!

It's hard to handle properly nullable on reference definition in a schema

here is a schema example snippet with labels nullable property reference:

      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  maxLength: 255
                  nullable: true
                  description: Snapshot name
                labels:
                  "$ref": "#/components/schemas/labels"
                  nullable: true
                  description: Resource labels
      operationId: update-example

For me, at the moment, I can only identify nullable through *base.SchemaProxy with GetReferenceNode(), and extract it from the native YAML.

From *base.Schema get from labels property *base.SchemaProxy with Schema(), the variable nullable is nil since it's pointing to the original reference schema.

Do you have a better solution to my issue, did I miss something? Or maybe it's something missing in the library?

Thank you

@daveshanley
Copy link
Member

The problem here is that currently the library will swap out everything in the node if there is a $ref provided. So description and nullable when used with a ref are removed. The only way to pick them back up is to drop down to the low level model and extract them from the yaml.Node This is not the behavior you're looking for, and this feature request exists, it's just not implemented yet.

@pierre-emmanuelJ
Copy link
Author

I'm getting the reference yaml node with GetReferenceNode() and currently it works for me:

Here is my current workaround

// https://github.com/pb33f/libopenapi/issues/283
func isNullableReference(node *yaml.Node) bool {
	if node == nil || node.Content == nil {
		return false
	}

	for i, c := range node.Content {
		if c.Value == "nullable" {
			if i+1 < len(node.Content) && node.Content[i+1].Value == "true" {
				return true
			}
		}
	}

	return false
}

call isNullableReference(properties.GetReferenceNode())

@daveshanley daveshanley added the documentation Improvements or additions to documentation label May 6, 2024
@daveshanley
Copy link
Member

Just an FYI, you can use utils.FindKeyNodeTop() to pull out the nullable key

https://github.com/pb33f/libopenapi/blob/main/utils/utils.go#L260

@pierre-emmanuelJ
Copy link
Author

Just an FYI, you can use utils.FindKeyNodeTop() to pull out the nullable key

https://github.com/pb33f/libopenapi/blob/main/utils/utils.go#L260

Thanks for the help :)

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

No branches or pull requests

2 participants