generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile OpenAPI spec to Go representation
- Loading branch information
Sergey Vilgelm
committed
Dec 23, 2021
1 parent
386f0c5
commit 583e558
Showing
64 changed files
with
7,243 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ permissions: | |
contents: write | ||
|
||
env: | ||
GO: "1.18beta1" | ||
GO: "1.18" | ||
|
||
jobs: | ||
bump-version: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
module github.com/sv-tools/openapi | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 | ||
github.com/stretchr/testify v1.7.0 | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 h1:TToq11gyfNlrMFZiYujSekIsPd9AmsA2Bj/iv+s4JHE= | ||
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package spec | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// BoolOrSchema handles Boolean or Schema type. | ||
// | ||
// It MUST be used as a pointer, | ||
// otherwise the `false` can be omitted by json or yaml encoders in case of `omitempty` tag is set. | ||
type BoolOrSchema struct { | ||
Allowed bool | ||
Schema *RefOrSpec[Schema] | ||
} | ||
|
||
// NewBoolOrSchema creates BoolOrSchema object. | ||
func NewBoolOrSchema(allowed bool, spec *RefOrSpec[Schema]) *BoolOrSchema { | ||
return &BoolOrSchema{ | ||
Allowed: allowed, | ||
Schema: spec, | ||
} | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler interface. | ||
func (o *BoolOrSchema) UnmarshalJSON(data []byte) error { | ||
if json.Unmarshal(data, &o.Allowed) == nil { | ||
o.Schema = nil | ||
return nil | ||
} | ||
if err := json.Unmarshal(data, &o.Schema); err != nil { | ||
return err | ||
} | ||
o.Allowed = true | ||
return nil | ||
} | ||
|
||
// MarshalJSON implements json.Marshaler interface. | ||
func (o *BoolOrSchema) MarshalJSON() ([]byte, error) { | ||
var v any | ||
if o.Schema != nil { | ||
v = o.Schema | ||
} else { | ||
v = o.Allowed | ||
} | ||
return json.Marshal(&v) | ||
} | ||
|
||
// UnmarshalYAML implements yaml.Unmarshaler interface. | ||
func (o *BoolOrSchema) UnmarshalYAML(node *yaml.Node) error { | ||
if node.Decode(&o.Allowed) == nil { | ||
o.Schema = nil | ||
return nil | ||
} | ||
if err := node.Decode(&o.Schema); err != nil { | ||
return err | ||
} | ||
o.Allowed = true | ||
return nil | ||
} | ||
|
||
// MarshalYAML implements yaml.Marshaler interface. | ||
func (o BoolOrSchema) MarshalYAML() (any, error) { | ||
var v any | ||
if o.Schema != nil { | ||
v = o.Schema | ||
} else { | ||
v = o.Allowed | ||
} | ||
|
||
return v, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package spec_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/sv-tools/openapi/spec" | ||
) | ||
|
||
type testAD struct { | ||
Name string `json:"name,omitempty" yaml:"name,omitempty"` | ||
AP *spec.BoolOrSchema `json:"ap,omitempty" yaml:"ap,omitempty"` | ||
} | ||
|
||
func TestAdditionalPropertiesJSON(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
data string | ||
nilAP bool | ||
allowed bool | ||
nilSchema bool | ||
}{ | ||
{ | ||
name: "no AP", | ||
data: `{"name": "foo"}`, | ||
nilAP: true, | ||
}, | ||
{ | ||
name: "false", | ||
data: `{"name": "foo", "ap": false}`, | ||
nilAP: false, | ||
allowed: false, | ||
nilSchema: true, | ||
}, | ||
{ | ||
name: "true", | ||
data: `{"name": "foo", "ap": true}`, | ||
nilAP: false, | ||
allowed: true, | ||
nilSchema: true, | ||
}, | ||
{ | ||
name: "schema", | ||
data: `{"name": "foo", "ap": {"title": "bar", "description": "test"}}`, | ||
nilAP: false, | ||
allowed: true, | ||
nilSchema: false, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var j testAD | ||
require.NoError(t, json.Unmarshal([]byte(tt.data), &j)) | ||
require.Equal(t, "foo", j.Name) | ||
if tt.nilAP { | ||
require.Nil(t, j.AP) | ||
} else { | ||
require.NotNil(t, j.AP) | ||
require.Equal(t, tt.allowed, j.AP.Allowed) | ||
require.Equal(t, tt.nilSchema, j.AP.Schema == nil) | ||
} | ||
newJson, err := json.Marshal(&j) | ||
require.NoError(t, err) | ||
require.JSONEq(t, tt.data, string(newJson)) | ||
|
||
var y testAD | ||
require.NoError(t, yaml.Unmarshal([]byte(tt.data), &y)) | ||
require.Equal(t, "foo", y.Name) | ||
if tt.nilAP { | ||
require.Nil(t, y.AP) | ||
} else { | ||
require.NotNil(t, y.AP) | ||
require.Equal(t, tt.allowed, y.AP.Allowed) | ||
require.Equal(t, tt.nilSchema, y.AP.Schema == nil) | ||
} | ||
newYaml, err := yaml.Marshal(&y) | ||
require.NoError(t, err) | ||
require.YAMLEq(t, tt.data, string(newYaml)) | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package spec | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Callback is a map of possible out-of band callbacks related to the parent operation. | ||
// Each value in the map is a Path Item Object that describes a set of requests that may be initiated by | ||
// the API provider and the expected responses. | ||
// The key value used to identify the path item object is an expression, evaluated at runtime, | ||
// that identifies a URL to use for the callback operation. | ||
// To describe incoming requests from the API provider independent from another API call, use the webhooks field. | ||
// | ||
// https://spec.openapis.org/oas/v3.1.0#callback-object | ||
// | ||
// Example: | ||
// myCallback: | ||
// '{$request.query.queryUrl}': | ||
// post: | ||
// requestBody: | ||
// description: Callback payload | ||
// content: | ||
// 'application/json': | ||
// schema: | ||
// $ref: '#/components/schemas/SomePayload' | ||
// responses: | ||
// '200': | ||
// description: callback successfully processed | ||
type Callback struct { | ||
Callback map[string]*RefOrSpec[Extendable[PathItem]] | ||
} | ||
|
||
// NewCallbackSpec creates Callback object. | ||
func NewCallbackSpec() *RefOrSpec[Extendable[Callback]] { | ||
o := make(map[string]*RefOrSpec[Extendable[PathItem]]) | ||
spec := NewExtendable(&Callback{ | ||
Callback: o, | ||
}) | ||
return NewRefOrSpec[Extendable[Callback]](nil, spec) | ||
} | ||
|
||
// NewCallbackRef creates Ref object. | ||
func NewCallbackRef(ref *Ref) *RefOrSpec[Extendable[Callback]] { | ||
return NewRefOrSpec[Extendable[Callback]](ref, nil) | ||
} | ||
|
||
// MarshalJSON implements json.Marshaler interface. | ||
func (o *Callback) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(&o.Callback) | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler interface. | ||
func (o *Callback) UnmarshalJSON(data []byte) error { | ||
return json.Unmarshal(data, &o.Callback) | ||
} | ||
|
||
// MarshalYAML implements yaml.Marshaler interface. | ||
func (o *Callback) MarshalYAML() (any, error) { | ||
return o.Callback, nil | ||
} | ||
|
||
// UnmarshalYAML implements yaml.Unmarshaler interface. | ||
func (o *Callback) UnmarshalYAML(node *yaml.Node) error { | ||
return node.Decode(&o.Callback) | ||
} | ||
|
||
func (o Callback) OpenAPIConstraint() {} |
Oops, something went wrong.