libopenapi icon indicating copy to clipboard operation
libopenapi copied to clipboard

nullable reference: hard to properly identify it

Open pierre-emmanuelJ opened this issue 9 months ago • 4 comments

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

pierre-emmanuelJ avatar May 01 '24 10:05 pierre-emmanuelJ

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.

daveshanley avatar May 04 '24 13:05 daveshanley

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())

pierre-emmanuelJ avatar May 06 '24 09:05 pierre-emmanuelJ

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

daveshanley avatar May 06 '24 21:05 daveshanley

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 :)

pierre-emmanuelJ avatar May 07 '24 07:05 pierre-emmanuelJ