From a4964c865a733e3f1625b8663875cb12c7934aa9 Mon Sep 17 00:00:00 2001 From: Alex Barter Date: Sat, 15 Jun 2024 20:50:40 +0000 Subject: [PATCH] Add example to README.md --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index aea156c0d..043a6e252 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,31 @@ func safeErrorMessage(err *openapi3.SchemaError) string { } ``` +## Reconciling component $ref types + +`ReferencesComponentInRootDocument` is a useful helper function to check if a component reference +coincides with a reference in the root document's component objects fixed fields. + +This can be used to determine if two schema definitions are of the same structure, helpful for +code generation tools when generating go type models. + +```go +doc, err = loader.LoadFromFile("openapi.yml") + +for _, path := range doc.Paths.InMatchingOrder() { + pathItem := doc.Paths.Find(path) + + if pathItem.Get == nil || pathItem.Get.Responses.Status(200) { + continue + } + + for _, s := range pathItem.Get.Responses.Status(200).Value.Content { + name, match := ReferencesComponentInRootDocument(doc, s.Schema) + fmt.Println(path, match, name) // /record true #/components/schemas/BookRecord + } +} +``` + This will change the schema validation errors to return only the `Reason` field, which is guaranteed to not include the original value. ## CHANGELOG: Sub-v1 breaking API changes