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
362 stars 46 forks source link

Panic During Schema Rendering #285

Open matteogastaldello opened 1 month ago

matteogastaldello commented 1 month ago

Hi! I'm encountering a runtime panic due to an invalid memory address or nil pointer dereference when attempting to render a schema using the libopenapi library. This issue arises during the execution of the schema.Render() method, which results in a segmentation violation error.

These are the lines that throws the panic

    schema, err = bodySchema.BuildSchema()
    if err != nil {
        return fmt.Errorf("building schema for %s: %w", verb.Path, err), errors
    }
    schema.Properties.Set("authenticationRefs", base.CreateSchemaProxy(&base.Schema{
        Type:        []string{"object"},
        Description: "AuthenticationRefs represent the reference to a CR containing the authentication information. One authentication method must be set."}))
    schema.Required = append(schema.Required, []string{"authenticationRefs"}...)
    rend, err := schema.Render()
    if err != nil {
        return fmt.Errorf("rendering schema for %s: %w", verb.Path, err), errors
    }

This error occurs if schema.Required is not previously set in the oas specification used to build the model. Setting a "fake" required field in the oas specification file fix the behaviour.

Thanks for your help

daveshanley commented 4 weeks ago

Hi,

What will really help me diagnose this problem is a blob of code I can copy and paste into my test-rig and debug the issue, the problem with snippets like this is that I have no idea how your source is structured or your base specification etc.

I really don't have the luxury of time to re-create this issue by reverse engineering the code you've provided. I really need something that will re-create the issue, that runs as a test or that will run as a copy paste job.

Thanks.

matteogastaldello commented 3 weeks ago

Hi @daveshanley!

Here is the gist with all the information to reproduce the error: https://gist.github.com/matteogastaldello/823cdaf0127c2f2eaa92ddb574539825

Thanks a lot!