mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 20:47:45 +00:00
Added polymorphic/array ignore to document config.
Now a document can be pre-configured to ignore polymorphic circular references, and array references. Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
@@ -40,6 +40,18 @@ type DocumentConfiguration struct {
|
||||
// BypassDocumentCheck will bypass the document check. This is disabled by default. This will allow any document to
|
||||
// passed in and used. Only enable this when parsing non openapi documents.
|
||||
BypassDocumentCheck bool
|
||||
|
||||
// IgnorePolymorphicCircularReferences will skip over checking for circular references in polymorphic schemas.
|
||||
// A polymorphic schema is any schema that is composed other schemas using references via `oneOf`, `anyOf` of `allOf`.
|
||||
// This is disabled by default, which means polymorphic circular references will be checked.
|
||||
IgnorePolymorphicCircularReferences bool
|
||||
|
||||
// IgnoreArrayCircularReferences will skip over checking for circular references in arrays. Sometimes a circular
|
||||
// reference is required to describe a data-shape correctly. Often those shapes are valid circles if the
|
||||
// type of the schema implementing the loop is an array. An empty array would technically break the loop.
|
||||
// So if libopenapi is returning circular references for this use case, then this option should be enabled.
|
||||
// this is disabled by default, which means array circular references will be checked.
|
||||
IgnoreArrayCircularReferences bool
|
||||
}
|
||||
|
||||
func NewOpenDocumentConfiguration() *DocumentConfiguration {
|
||||
|
||||
@@ -63,6 +63,16 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
||||
|
||||
// create resolver and check for circular references.
|
||||
resolve := resolver.NewResolver(idx)
|
||||
|
||||
// if configured, ignore circular references in arrays and polymorphic schemas
|
||||
if config.IgnoreArrayCircularReferences {
|
||||
resolve.IgnoreArrayCircularReferences()
|
||||
}
|
||||
if config.IgnorePolymorphicCircularReferences {
|
||||
resolve.IgnorePolymorphicCircularReferences()
|
||||
}
|
||||
|
||||
// check for circular references.
|
||||
resolvingErrors := resolve.CheckForCircularReferences()
|
||||
|
||||
if len(resolvingErrors) > 0 {
|
||||
|
||||
@@ -790,3 +790,71 @@ paths:
|
||||
|
||||
assert.Equal(t, spec, strings.TrimSpace(string(rend)))
|
||||
}
|
||||
|
||||
func TestDocument_IgnorePolymorphicCircularReferences(t *testing.T) {
|
||||
|
||||
var d = `openapi: 3.1.0
|
||||
components:
|
||||
schemas:
|
||||
ProductCategory:
|
||||
type: "object"
|
||||
properties:
|
||||
name:
|
||||
type: "string"
|
||||
children:
|
||||
type: "object"
|
||||
anyOf:
|
||||
- $ref: "#/components/schemas/ProductCategory"
|
||||
description: "Array of sub-categories in the same format."
|
||||
required:
|
||||
- "name"
|
||||
- "children"`
|
||||
|
||||
config := datamodel.NewClosedDocumentConfiguration()
|
||||
config.IgnorePolymorphicCircularReferences = true
|
||||
|
||||
doc, err := NewDocumentWithConfiguration([]byte(d), config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m, errs := doc.BuildV3Model()
|
||||
|
||||
assert.Len(t, errs, 0)
|
||||
assert.Len(t, m.Index.GetCircularReferences(), 0)
|
||||
|
||||
}
|
||||
|
||||
func TestDocument_IgnoreArrayCircularReferences(t *testing.T) {
|
||||
|
||||
var d = `openapi: 3.1.0
|
||||
components:
|
||||
schemas:
|
||||
ProductCategory:
|
||||
type: "object"
|
||||
properties:
|
||||
name:
|
||||
type: "string"
|
||||
children:
|
||||
type: "array"
|
||||
items:
|
||||
$ref: "#/components/schemas/ProductCategory"
|
||||
description: "Array of sub-categories in the same format."
|
||||
required:
|
||||
- "name"
|
||||
- "children"`
|
||||
|
||||
config := datamodel.NewClosedDocumentConfiguration()
|
||||
config.IgnoreArrayCircularReferences = true
|
||||
|
||||
doc, err := NewDocumentWithConfiguration([]byte(d), config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m, errs := doc.BuildV3Model()
|
||||
|
||||
assert.Len(t, errs, 0)
|
||||
assert.Len(t, m.Index.GetCircularReferences(), 0)
|
||||
|
||||
}
|
||||
|
||||
@@ -105,12 +105,6 @@ type SpecIndexConfig struct {
|
||||
// Use the `BuildIndex()` method on the index to build it out once resolved/ready.
|
||||
AvoidBuildIndex bool
|
||||
|
||||
// If set to true, the index will ignore circular references in polymorphic schemas.
|
||||
IgnorePolymorphicCircularReferences bool // ignore circular references in polymorphic schemas.
|
||||
|
||||
// If set to true, the index will ignore circular references in arrays.
|
||||
IgnoreArrayCircularReferences bool // ignore circular references in arrays.
|
||||
|
||||
// private fields
|
||||
seenRemoteSources *syncmap.Map
|
||||
remoteLock *sync.Mutex
|
||||
|
||||
Reference in New Issue
Block a user