-
-
Notifications
You must be signed in to change notification settings - Fork 65
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
Quickstart tutorial leads to nil pointer dereference error
#308
Comments
Thank you for this work. I like your idea. |
The code should read like this. I will update it and add to the quick start. package main
import (
"fmt"
"github.com/pb33f/libopenapi"
"os"
)
func main() {
petstore, _ := os.ReadFile("petstorev3.json")
document, err := libopenapi.NewDocument(petstore)
if err != nil {
panic(fmt.Sprintf("cannot create new document: %e", err))
}
docModel, errors := document.BuildV3Model()
if len(errors) > 0 {
for i := range errors {
fmt.Printf("error: %e\n", errors[i])
}
panic(fmt.Sprintf("cannot create v3 model from document: %d errors reported", len(errors)))
}
// The following fails after the first iteration
for schemaPairs := docModel.Model.Components.Schemas.First(); schemaPairs != nil; schemaPairs = schemaPairs.Next() {
// get the name of the schema
schemaName := schemaPairs.Key()
// get the schema object from the map
schemaValue := schemaPairs.Value()
// build the schema
schema := schemaValue.Schema()
// if the schema has properties, print the number of properties
if schema != nil && schema.Properties != nil {
fmt.Printf("Schema '%s' has %d properties\n", schemaName, schema.Properties.Len())
}
}
} |
Readme has been updated with this exact code walk through, thank you for the nudge. Also the blog post has been updated. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are linking to Parsing OpenAPI files using go guide as the Quick-start tutorial for libopenapi.
Following the tutorial (while fixing the three different spellings of
docModel
,docmodel
andmodel
) leads to apanic: runtime error: invalid memory address or nil pointer dereference
when printing the schemas. Steps to reproduce:curl https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.yaml > petstorev3.json
go get github.com/pb33f/libopenapi
main.go
file:go run main.go
This produces
Tested with
v0.16.10
of libopenapi and1.0.0
of the Swagger Petstore specification.This error is expected, as
Pets
does not have any properties as it is an array ofPet
.Maybe you could just add a small snippet as the quickstart guide instead of the the external tutorial.
The text was updated successfully, but these errors were encountered: