Skip to content

Commit

Permalink
fix: eliminated race condition, buildProps() no longer needs to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 authored and daveshanley committed Jun 30, 2023
1 parent f790174 commit f77a8f6
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions datamodel/high/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
package base

import (
"gopkg.in/yaml.v3"
"sync"

"github.com/pb33f/libopenapi/datamodel/high"
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/base"
"gopkg.in/yaml.v3"
)

// Schema represents a JSON Schema that support Swagger, OpenAPI 3 and OpenAPI 3.1
Expand Down Expand Up @@ -315,7 +313,6 @@ func NewSchema(schema *base.Schema) *Schema {
// any properties each need to be processed in their own thread.
// we go as fast as we can.
polyCompletedChan := make(chan bool)
propsChan := make(chan bool)
errChan := make(chan error)

type buildResult struct {
Expand Down Expand Up @@ -355,19 +352,16 @@ func NewSchema(schema *base.Schema) *Schema {
}

// props async
var plock sync.Mutex
buildProps := func(k lowmodel.KeyReference[string], v lowmodel.ValueReference[*base.SchemaProxy], c chan bool,
buildProps := func(k lowmodel.KeyReference[string], v lowmodel.ValueReference[*base.SchemaProxy],
props map[string]*SchemaProxy, sw int,
) {
plock.Lock()
props[k.Value] = &SchemaProxy{
schema: &lowmodel.NodeReference[*base.SchemaProxy]{
Value: v.Value,
KeyNode: k.KeyNode,
ValueNode: v.ValueNode,
},
}
plock.Unlock()

switch sw {
case 0:
Expand All @@ -377,21 +371,20 @@ func NewSchema(schema *base.Schema) *Schema {
case 2:
s.PatternProperties = props
}
c <- true
}

props := make(map[string]*SchemaProxy)
for k, v := range schema.Properties.Value {
go buildProps(k, v, propsChan, props, 0)
buildProps(k, v, props, 0)
}

dependents := make(map[string]*SchemaProxy)
for k, v := range schema.DependentSchemas.Value {
go buildProps(k, v, propsChan, dependents, 1)
buildProps(k, v, dependents, 1)
}
patternProps := make(map[string]*SchemaProxy)
for k, v := range schema.PatternProperties.Value {
go buildProps(k, v, propsChan, patternProps, 2)
buildProps(k, v, patternProps, 2)
}

var allOf []*SchemaProxy
Expand Down Expand Up @@ -439,7 +432,7 @@ func NewSchema(schema *base.Schema) *Schema {

completeChildren := 0
completedProps := 0
totalProps := len(schema.Properties.Value) + len(schema.DependentSchemas.Value) + len(schema.PatternProperties.Value)
totalProps := len(schema.DependentSchemas.Value) + len(schema.PatternProperties.Value)
if totalProps+children > 0 {
allDone:
for true {
Expand All @@ -449,11 +442,6 @@ func NewSchema(schema *base.Schema) *Schema {
if totalProps == completedProps && children == completeChildren {
break allDone
}
case <-propsChan:
completedProps++
if totalProps == completedProps && children == completeChildren {
break allDone
}
}
}
}
Expand Down

0 comments on commit f77a8f6

Please sign in to comment.