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
482 stars 64 forks source link

SchemaTypeRef field wrong set nil #314

Open leyserai opened 3 months ago

leyserai commented 3 months ago

hello, When I parse a swagger json, the request body refer to an object, such as #/components/schemas/TeacherAdmitWay, but the SchemaTypeRef in pathItem.Post.RequestBody.Schema is nil.

the attachment is the json file. the code below


document, err := libopenapi.NewDocument([]byte(fiddleCommon.SwaggerJson))
    if err != nil {
        panic(fmt.Sprintf("create document failed: %v", err))
    }

    document.SetConfiguration(&datamodel.DocumentConfiguration{
        BundleInlineRefs: true,
    })
    docModel, errors := document.BuildV3Model()
    if len(errors) > 0 {
        for _, err := range errors {
            fmt.Printf("error: %v\n", err)
        }
        panic(fmt.Sprintf("errors %d", len(errors)))
    }
for pair := docModel.Model.Paths.PathItems.First(); pair != nil; pair = pair.Next() {
        path := pair.Key()
        pathItem := pair.Value()
        fmt.Printf("路径: %s\n", path)

        if pathItem.Post != nil {
            fmt.Printf(" %s\n", pathItem.Post.Description)

            if pathItem.Post.RequestBody != nil {
                parseRequestBody(pathItem.Post.RequestBody, &docModel.Model)
            }
             }
func parseRequestBody(requestBody *v3.RequestBody, docModel *v3.Document) {
    if requestBody.Content != nil {
        for pair := requestBody.Content.First(); pair != nil; pair = pair.Next() {
            contentType := pair.Key()
            mediaType := pair.Value()
            fmt.Printf("      %s\n", contentType)

            if mediaType.Schema != nil {
                parseSchema(mediaType.Schema.Schema(), "        ", docModel)
            }
    if schema.SchemaTypeRef != "" {
        fmt.Printf("%s: %s\n", indent, schema.SchemaTypeRef)
        refSchema := resolveRef(schema.SchemaTypeRef, docModel)
        if refSchema != nil {
            parseSchema(refSchema, indent+"  ", docModel)
        }

    }
        }
    }
}

In Schema.Build method, the code blow _, schemaRefLabel, schemaRefNode := utils.FindKeyNodeFullTop(SchemaTypeLabel, root.Content) SchemaTypeLabel is "$schema", but not found in the json file.

attache json file: test.json

petkostas commented 2 months ago

Could you provide a simplified example? Also, why do you use refSchema: = resolveRef (schema. SchemaTypeRef, docModel)? The library provides a proxy to fetch the schema if it is a reference. I would understand the logic if it was a polymorphic schema (oneOf, allOf, anyOf), but I am missing the example.