From 781ec3c189c73f57293d405d2b2d968c83d21de8 Mon Sep 17 00:00:00 2001 From: quobix Date: Mon, 9 Sep 2024 12:39:49 -0400 Subject: [PATCH] Added all component schema to prevent allocations. This function runs a lot and `syncMapToMap` is slow. The function is idepmotent so a cache prevents a lot of un-nessecary burn. --- index/index_model.go | 1 + index/spec_index.go | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/index/index_model.go b/index/index_model.go index 02c8707..ae8cdc4 100644 --- a/index/index_model.go +++ b/index/index_model.go @@ -254,6 +254,7 @@ type SpecIndex struct { allComponentSchemaDefinitions *sync.Map // all schemas found in components (openapi) or definitions (swagger). securitySchemesNode *yaml.Node // components/securitySchemes node allSecuritySchemes map[string]*Reference // all security schemes / definitions. + allComponentSchemas map[string]*Reference // all component schemas requestBodiesNode *yaml.Node // components/requestBodies node allRequestBodies map[string]*Reference // all request bodies responsesNode *yaml.Node // components/responses node diff --git a/index/spec_index.go b/index/spec_index.go index ad146f6..36f13a4 100644 --- a/index/spec_index.go +++ b/index/spec_index.go @@ -328,7 +328,15 @@ func (index *SpecIndex) GetAllReferenceSchemas() []*Reference { // GetAllComponentSchemas will return all schemas defined in the components section of the document. func (index *SpecIndex) GetAllComponentSchemas() map[string]*Reference { - return syncMapToMap[string, *Reference](index.allComponentSchemaDefinitions) + if index != nil && index.allComponentSchemas != nil { + return index.allComponentSchemas + } + if index != nil && index.allComponentSchemas == nil { + schemaMap := syncMapToMap[string, *Reference](index.allComponentSchemaDefinitions) + index.allComponentSchemas = schemaMap + return index.allComponentSchemas + } + return nil } // GetAllSecuritySchemes will return all security schemes / definitions found in the document.