Skip to content

Commit

Permalink
Merge pull request #205 from speakeasy-api/ordered-libopenapi
Browse files Browse the repository at this point in the history
feat: introduce ordered maps into libopenapi
  • Loading branch information
daveshanley authored Dec 15, 2023
2 parents 7917e6c + f86d7bc commit a912093
Show file tree
Hide file tree
Showing 216 changed files with 6,548 additions and 5,032 deletions.
11 changes: 6 additions & 5 deletions datamodel/high/base/discriminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package base
import (
low2 "github.com/pb33f/libopenapi/datamodel/high"
low "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/orderedmap"
"gopkg.in/yaml.v3"
)

Expand All @@ -19,8 +20,8 @@ import (
//
// v3 - https://spec.openapis.org/oas/v3.1.0#discriminator-object
type Discriminator struct {
PropertyName string `json:"propertyName,omitempty" yaml:"propertyName,omitempty"`
Mapping map[string]string `json:"mapping,omitempty" yaml:"mapping,omitempty"`
PropertyName string `json:"propertyName,omitempty" yaml:"propertyName,omitempty"`
Mapping *orderedmap.Map[string, string] `json:"mapping,omitempty" yaml:"mapping,omitempty"`
low *low.Discriminator
}

Expand All @@ -29,9 +30,9 @@ func NewDiscriminator(disc *low.Discriminator) *Discriminator {
d := new(Discriminator)
d.low = disc
d.PropertyName = disc.PropertyName.Value
mapping := make(map[string]string)
for k, v := range disc.Mapping.Value {
mapping[k.Value] = v.Value
mapping := orderedmap.New[string, string]()
for pair := orderedmap.First(disc.Mapping.Value); pair != nil; pair = pair.Next() {
mapping.Set(pair.Key().Value, pair.Value().Value)
}
d.Mapping = mapping
return d
Expand Down
12 changes: 5 additions & 7 deletions datamodel/high/base/discriminator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ package base

import (
"fmt"
"strings"
"testing"

lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"strings"
"testing"
)

func TestNewDiscriminator(t *testing.T) {

var cNode yaml.Node

yml := `propertyName: coffee
Expand All @@ -31,17 +31,15 @@ mapping:
highDiscriminator := NewDiscriminator(&lowDiscriminator)

assert.Equal(t, "coffee", highDiscriminator.PropertyName)
assert.Equal(t, "in the morning", highDiscriminator.Mapping["fogCleaner"])
assert.Equal(t, "in the morning", highDiscriminator.Mapping.GetOrZero("fogCleaner"))
assert.Equal(t, 3, highDiscriminator.GoLow().FindMappingValue("fogCleaner").ValueNode.Line)

// render the example as YAML
rendered, _ := highDiscriminator.Render()
assert.Equal(t, strings.TrimSpace(string(rendered)), yml)

}

func ExampleNewDiscriminator() {

// create a yaml representation of a discriminator (can be JSON, doesn't matter)
yml := `propertyName: coffee
mapping:
Expand All @@ -59,6 +57,6 @@ mapping:
highDiscriminator := NewDiscriminator(&lowDiscriminator)

// print out a mapping defined for the discriminator.
fmt.Print(highDiscriminator.Mapping["coffee"])
fmt.Print(highDiscriminator.Mapping.GetOrZero("coffee"))
// Output: in the morning
}
10 changes: 3 additions & 7 deletions datamodel/high/base/dynamic_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ package base

import (
"context"
"strings"
"testing"

"github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"strings"
"testing"
)

func TestDynamicValue_Render_A(t *testing.T) {
Expand Down Expand Up @@ -57,7 +58,6 @@ func TestDynamicValue_Render_Float64(t *testing.T) {
}

func TestDynamicValue_Render_Ptr(t *testing.T) {

type cake struct {
Cake string
}
Expand All @@ -68,7 +68,6 @@ func TestDynamicValue_Render_Ptr(t *testing.T) {
}

func TestDynamicValue_Render_PtrRenderable(t *testing.T) {

tag := &Tag{
Name: "cake",
}
Expand All @@ -79,7 +78,6 @@ func TestDynamicValue_Render_PtrRenderable(t *testing.T) {
}

func TestDynamicValue_RenderInline(t *testing.T) {

tag := &Tag{
Name: "cake",
}
Expand All @@ -90,7 +88,6 @@ func TestDynamicValue_RenderInline(t *testing.T) {
}

func TestDynamicValue_MarshalYAMLInline(t *testing.T) {

const ymlComponents = `components:
schemas:
rice:
Expand Down Expand Up @@ -134,7 +131,6 @@ func TestDynamicValue_MarshalYAMLInline(t *testing.T) {
}

func TestDynamicValue_MarshalYAMLInline_Error(t *testing.T) {

const ymlComponents = `components:
schemas:
rice:
Expand Down
19 changes: 10 additions & 9 deletions datamodel/high/base/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
"github.com/pb33f/libopenapi/datamodel/high"
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
low "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/orderedmap"
"gopkg.in/yaml.v3"
)

// Example represents a high-level Example object as defined by OpenAPI 3+
//
// v3 - https://spec.openapis.org/oas/v3.1.0#example-object
type Example struct {
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Value any `json:"value,omitempty" yaml:"value,omitempty"`
ExternalValue string `json:"externalValue,omitempty" yaml:"externalValue,omitempty"`
Extensions map[string]any `json:"-" yaml:"-"`
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Value *yaml.Node `json:"value,omitempty" yaml:"value,omitempty"`
ExternalValue string `json:"externalValue,omitempty" yaml:"externalValue,omitempty"`
Extensions *orderedmap.Map[string, *yaml.Node] `json:"-" yaml:"-"`
low *low.Example
}

Expand Down Expand Up @@ -57,10 +58,10 @@ func (e *Example) MarshalYAML() (interface{}, error) {

// ExtractExamples will convert a low-level example map, into a high level one that is simple to navigate.
// no fidelity is lost, everything is still available via GoLow()
func ExtractExamples(elements map[lowmodel.KeyReference[string]]lowmodel.ValueReference[*low.Example]) map[string]*Example {
extracted := make(map[string]*Example)
for k, v := range elements {
extracted[k.Value] = NewExample(v.Value)
func ExtractExamples(elements *orderedmap.Map[lowmodel.KeyReference[string], lowmodel.ValueReference[*low.Example]]) *orderedmap.Map[string, *Example] {
extracted := orderedmap.New[string, *Example]()
for pair := orderedmap.First(elements); pair != nil; pair = pair.Next() {
extracted.Set(pair.Key().Value, NewExample(pair.Value().Value))
}
return extracted
}
36 changes: 19 additions & 17 deletions datamodel/high/base/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ package base
import (
"context"
"fmt"
"strings"
"testing"

lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"strings"
"testing"
)

func TestNewExample(t *testing.T) {

var cNode yaml.Node

yml := `summary: an example
Expand All @@ -35,18 +36,23 @@ x-hack: code`
// build high
highExample := NewExample(&lowExample)

var xHack string
_ = highExample.Extensions.GetOrZero("x-hack").Decode(&xHack)

var example string
_ = highExample.Value.Decode(&example)

assert.Equal(t, "an example", highExample.Summary)
assert.Equal(t, "something more", highExample.Description)
assert.Equal(t, "https://pb33f.io", highExample.ExternalValue)
assert.Equal(t, "code", highExample.Extensions["x-hack"])
assert.Equal(t, "a thing", highExample.Value)
assert.Equal(t, "code", xHack)
assert.Equal(t, "a thing", example)
assert.Equal(t, 4, highExample.GoLow().ExternalValue.ValueNode.Line)
assert.NotNil(t, highExample.GoLowUntyped())

// render the example as YAML
rendered, _ := highExample.Render()
assert.Equal(t, strings.TrimSpace(string(rendered)), yml)

assert.Equal(t, yml, strings.TrimSpace(string(rendered)))
}

func TestExtractExamples(t *testing.T) {
Expand All @@ -62,19 +68,16 @@ func TestExtractExamples(t *testing.T) {

_ = lowExample.Build(context.Background(), nil, cNode.Content[0], nil)

examplesMap := make(map[lowmodel.KeyReference[string]]lowmodel.ValueReference[*lowbase.Example])
examplesMap[lowmodel.KeyReference[string]{
Value: "green",
}] = lowmodel.ValueReference[*lowbase.Example]{
Value: &lowExample,
}

assert.Equal(t, "herbs", ExtractExamples(examplesMap)["green"].Summary)
examplesMap := orderedmap.New[lowmodel.KeyReference[string], lowmodel.ValueReference[*lowbase.Example]]()
examplesMap.Set(
lowmodel.KeyReference[string]{Value: "green"},
lowmodel.ValueReference[*lowbase.Example]{Value: &lowExample},
)

assert.Equal(t, "herbs", ExtractExamples(examplesMap).GetOrZero("green").Summary)
}

func ExampleNewExample() {

// create some example yaml (or can be JSON, it does not matter)
yml := `summary: something interesting
description: something more interesting with detail
Expand All @@ -97,5 +100,4 @@ x-hack: code`

fmt.Print(highExample.ExternalValue)
// Output: https://pb33f.io

}
9 changes: 5 additions & 4 deletions datamodel/high/base/external_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package base
import (
"github.com/pb33f/libopenapi/datamodel/high"
low "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/orderedmap"
"gopkg.in/yaml.v3"
)

Expand All @@ -16,9 +17,9 @@ import (
// v2 - https://swagger.io/specification/v2/#externalDocumentationObject
// v3 - https://spec.openapis.org/oas/v3.1.0#external-documentation-object
type ExternalDoc struct {
Description string `json:"description,omitempty" yaml:"description,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Extensions map[string]any `json:"-" yaml:"-"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Extensions *orderedmap.Map[string, *yaml.Node] `json:"-" yaml:"-"`
low *low.ExternalDoc
}

Expand Down Expand Up @@ -46,7 +47,7 @@ func (e *ExternalDoc) GoLowUntyped() any {
return e.low
}

func (e *ExternalDoc) GetExtensions() map[string]any {
func (e *ExternalDoc) GetExtensions() *orderedmap.Map[string, *yaml.Node] {
return e.Extensions
}

Expand Down
26 changes: 14 additions & 12 deletions datamodel/high/base/external_doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ package base

import (
"context"
"fmt"
"strings"
"testing"

lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"strings"
"testing"
)

func TestNewExternalDoc(t *testing.T) {

var cNode yaml.Node

yml := `description: hack code
Expand All @@ -31,22 +31,23 @@ x-hack: code`

highExt := NewExternalDoc(&lowExt)

var xHack string
_ = highExt.Extensions.GetOrZero("x-hack").Decode(&xHack)

assert.Equal(t, "hack code", highExt.Description)
assert.Equal(t, "https://pb33f.io", highExt.URL)
assert.Equal(t, "code", highExt.Extensions["x-hack"])
assert.Equal(t, "code", xHack)

wentLow := highExt.GoLow()
assert.Equal(t, 2, wentLow.URL.ValueNode.Line)
assert.Len(t, highExt.GetExtensions(), 1)
assert.Equal(t, 1, orderedmap.Len(highExt.GetExtensions()))

// render the high-level object as YAML
rendered, _ := highExt.Render()
assert.Equal(t, strings.TrimSpace(string(rendered)), yml)

}

func ExampleNewExternalDoc() {

func TestExampleNewExternalDoc(t *testing.T) {
// create a new external documentation spec reference
// this can be YAML or JSON.
yml := `description: hack code docs
Expand All @@ -67,7 +68,8 @@ x-hack: code`
// create new high-level ExternalDoc
highExt := NewExternalDoc(&lowExt)

// print out a extension
fmt.Print(highExt.Extensions["x-hack"])
// Output: code
var xHack string
_ = highExt.Extensions.GetOrZero("x-hack").Decode(&xHack)

assert.Equal(t, "code", xHack)
}
19 changes: 10 additions & 9 deletions datamodel/high/base/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package base
import (
"github.com/pb33f/libopenapi/datamodel/high"
low "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/orderedmap"
"gopkg.in/yaml.v3"
)

Expand All @@ -17,14 +18,14 @@ import (
// v2 - https://swagger.io/specification/v2/#infoObject
// v3 - https://spec.openapis.org/oas/v3.1.0#info-object
type Info struct {
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
TermsOfService string `json:"termsOfService,omitempty" yaml:"termsOfService,omitempty"`
Contact *Contact `json:"contact,omitempty" yaml:"contact,omitempty"`
License *License `json:"license,omitempty" yaml:"license,omitempty"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Extensions map[string]any
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
TermsOfService string `json:"termsOfService,omitempty" yaml:"termsOfService,omitempty"`
Contact *Contact `json:"contact,omitempty" yaml:"contact,omitempty"`
License *License `json:"license,omitempty" yaml:"license,omitempty"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Extensions *orderedmap.Map[string, *yaml.Node] `json:"-" yaml:"-"`
low *low.Info
}

Expand Down Expand Up @@ -53,7 +54,7 @@ func NewInfo(info *low.Info) *Info {
if !info.Version.IsEmpty() {
i.Version = info.Version.Value
}
if len(info.Extensions) > 0 {
if orderedmap.Len(info.Extensions) > 0 {
i.Extensions = high.ExtractExtensions(info.Extensions)
}
return i
Expand Down
Loading

0 comments on commit a912093

Please sign in to comment.