Skip to content

Commit

Permalink
Add example to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
percivalalb committed Jun 15, 2024
1 parent c3fe41b commit a4964c8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a4964c8

Please sign in to comment.