pb33f / libopenapi

libopenapi is a fully featured, high performance OpenAPI 3.1, 3.0 and Swagger parser, library, validator and toolkit for golang applications.
https://pb33f.io/libopenapi/
Other
350 stars 43 forks source link

nullable reference: hard to properly identify it #283

Open pierre-emmanuelJ opened 2 weeks ago

pierre-emmanuelJ commented 2 weeks ago

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 commented 2 weeks ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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