Skip to content
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

SchemaTypeRef field wrong set nil #314

Open
leyserai opened this issue Aug 1, 2024 · 1 comment
Open

SchemaTypeRef field wrong set nil #314

leyserai opened this issue Aug 1, 2024 · 1 comment

Comments

@leyserai
Copy link

leyserai commented Aug 1, 2024

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants