Skip to content

Commit

Permalink
Merge pull request #170 from danielgtaylor/libopenapi-0.4
Browse files Browse the repository at this point in the history
fix: upgrade to libopenapi 0.4.x
  • Loading branch information
danielgtaylor authored Jan 23, 2023
2 parents ac909e0 + 03818b5 commit 1650eae
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.16
github.com/mitchellh/mapstructure v1.5.0
github.com/pb33f/libopenapi v0.3.7
github.com/pb33f/libopenapi v0.4.18-0.20230121110523-de1a42c72ff4
github.com/schollz/progressbar/v3 v3.12.2
github.com/shamaton/msgpack/v2 v2.1.1
github.com/spf13/afero v1.9.3
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.3.7 h1:5qvRwUedU0PM+hDrUJIHVcAKGJ9R3Ex2j/r7MExRgyc=
github.com/pb33f/libopenapi v0.3.7/go.mod h1:UcUNPQcwq4ojgQgthV+zbeUs25lhDlD4bM9Da8n2vdU=
github.com/pb33f/libopenapi v0.4.17 h1:Vnd2QTvJx4uDNHjtsYLGt6PGcbc3dNs3/GNvG9cULM8=
github.com/pb33f/libopenapi v0.4.17/go.mod h1:UcUNPQcwq4ojgQgthV+zbeUs25lhDlD4bM9Da8n2vdU=
github.com/pb33f/libopenapi v0.4.18-0.20230121110523-de1a42c72ff4 h1:EjxnLsplJtrXKu/HHha3q4+KjwwE1SwVjejK9/fCCoI=
github.com/pb33f/libopenapi v0.4.18-0.20230121110523-de1a42c72ff4/go.mod h1:UcUNPQcwq4ojgQgthV+zbeUs25lhDlD4bM9Da8n2vdU=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
Expand Down
26 changes: 11 additions & 15 deletions openapi/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/lucasjones/reggen"
"github.com/pb33f/libopenapi/datamodel/high/base"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"golang.org/x/exp/maps"
)

Expand Down Expand Up @@ -50,21 +49,21 @@ func genExampleInternal(s *base.Schema, mode schemaMode, known map[[32]byte]bool
}

if s.Minimum != nil {
if s.ExclusiveMinimumBool != nil && *s.ExclusiveMinimumBool {
if s.ExclusiveMinimum != nil && s.ExclusiveMinimum.IsA() && s.ExclusiveMinimum.A {
return *s.Minimum + 1
}
return *s.Minimum
} else if s.ExclusiveMinimum != nil {
return *s.ExclusiveMinimum + 1
} else if s.ExclusiveMinimum != nil && (s.ExclusiveMinimum.IsB()) {
return s.ExclusiveMinimum.B + 1
}

if s.Maximum != nil {
if s.ExclusiveMaximumBool != nil && *s.ExclusiveMaximumBool {
if s.ExclusiveMaximum != nil && s.ExclusiveMaximum.IsA() && s.ExclusiveMaximum.A {
return *s.Maximum - 1
}
return *s.Maximum
} else if s.ExclusiveMaximum != nil {
return *s.ExclusiveMaximum - 1
} else if s.ExclusiveMaximum != nil && s.ExclusiveMaximum.IsB() {
return s.ExclusiveMaximum.B - 1
}

if s.MultipleOf != nil && *s.MultipleOf != 0 {
Expand Down Expand Up @@ -153,8 +152,8 @@ func genExampleInternal(s *base.Schema, mode schemaMode, known map[[32]byte]bool

return "string"
case "array":
if len(s.Items) > 0 {
items := s.Items[0].Schema()
if s.Items != nil && s.Items.IsA() {
items := s.Items.A.Schema()
simple := isSimpleSchema(items)
hash := items.GoLow().Hash()
if simple || !known[hash] {
Expand Down Expand Up @@ -210,13 +209,10 @@ func genExampleInternal(s *base.Schema, mode schemaMode, known map[[32]byte]bool

if s.AdditionalProperties != nil {
ap := s.AdditionalProperties
if sp, ok := ap.(*lowbase.SchemaProxy); ok {
ap = sp.Schema()
}
if low, ok := ap.(*lowbase.Schema); ok {
addl := base.NewSchema(low)
if sp, ok := ap.(*base.SchemaProxy); ok {
addl := sp.Schema()
simple := isSimpleSchema(addl)
hash := low.Hash()
hash := addl.GoLow().Hash()
if simple || !known[hash] {
known[hash] = true
value["<any>"] = genExampleInternal(addl, mode, known)
Expand Down
4 changes: 2 additions & 2 deletions openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func openapiOperation(cmd *cobra.Command, method string, uriTemplate *url.URL, p
}

if typ == "array" {
if len(s.Items) > 0 {
items := s.Items[0].Schema()
if s.Items != nil && s.Items.IsA() {
items := s.Items.A.Schema()
if len(items.Type) > 0 {
typ += "[" + items.Type[0] + "]"
}
Expand Down
26 changes: 11 additions & 15 deletions openapi/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/pb33f/libopenapi/datamodel/high/base"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
)

type schemaMode int
Expand Down Expand Up @@ -104,22 +103,22 @@ func renderSchemaInternal(s *base.Schema, indent string, mode schemaMode, known

if s.Minimum != nil {
key := "min"
if s.ExclusiveMinimumBool != nil && *s.ExclusiveMinimumBool {
if s.ExclusiveMinimum != nil && s.ExclusiveMinimum.IsA() && s.ExclusiveMinimum.A {
key = "exclusiveMin"
}
tags = append(tags, fmt.Sprintf("%s:%d", key, *s.Minimum))
} else if s.ExclusiveMinimum != nil {
tags = append(tags, fmt.Sprintf("exclusiveMin:%d", *s.ExclusiveMinimum))
} else if s.ExclusiveMinimum != nil && s.ExclusiveMinimum.IsB() {
tags = append(tags, fmt.Sprintf("exclusiveMin:%d", s.ExclusiveMinimum.B))
}

if s.Maximum != nil {
key := "max"
if s.ExclusiveMaximumBool != nil && *s.ExclusiveMaximumBool {
if s.ExclusiveMaximum != nil && s.ExclusiveMaximum.IsA() && s.ExclusiveMaximum.A {
key = "exclusiveMax"
}
tags = append(tags, fmt.Sprintf("%s:%d", key, *s.Maximum))
} else if s.ExclusiveMaximum != nil {
tags = append(tags, fmt.Sprintf("exclusiveMax:%d", *s.ExclusiveMaximum))
} else if s.ExclusiveMaximum != nil && s.ExclusiveMaximum.IsB() {
tags = append(tags, fmt.Sprintf("exclusiveMax:%d", s.ExclusiveMaximum.B))
}

if s.MultipleOf != nil && *s.MultipleOf != 0 {
Expand Down Expand Up @@ -165,8 +164,8 @@ func renderSchemaInternal(s *base.Schema, indent string, mode schemaMode, known
}
return fmt.Sprintf("(%s%s)%s", strings.Join(s.Type, "|"), tagStr, doc)
case "array":
if len(s.Items) > 0 {
items := s.Items[0].Schema()
if s.Items != nil && s.Items.IsA() {
items := s.Items.A.Schema()
simple := isSimpleSchema(items)
hash := items.GoLow().Hash()
if simple || !known[hash] {
Expand Down Expand Up @@ -225,13 +224,10 @@ func renderSchemaInternal(s *base.Schema, indent string, mode schemaMode, known

if s.AdditionalProperties != nil {
ap := s.AdditionalProperties
if sp, ok := ap.(*lowbase.SchemaProxy); ok {
ap = sp.Schema()
}
if low, ok := ap.(*lowbase.Schema); ok {
addl := base.NewSchema(low)
if sp, ok := ap.(*base.SchemaProxy); ok {
addl := sp.Schema()
simple := isSimpleSchema(addl)
hash := low.Hash()
hash := addl.GoLow().Hash()
if simple || !known[hash] {
known[hash] = true
obj += indent + " " + "<any>: " + renderSchemaInternal(addl, indent+" ", mode, known) + "\n"
Expand Down

0 comments on commit 1650eae

Please sign in to comment.