Closed baderj closed 4 months ago
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.
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.