├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── cmd ├── oagen │ └── main.go └── oalint │ └── main.go ├── generator ├── grpcgen │ ├── grpcgen.go │ ├── grpcgen_test.go │ ├── interface.go │ ├── options.go │ └── protobuf.go └── restclientgen │ ├── compiler │ ├── call.go │ ├── compiler.go │ ├── interface.go │ └── types.go │ ├── es6flow │ ├── es6flow.go │ ├── interface.go │ └── options.go │ ├── golang │ ├── golang.go │ ├── interface.go │ └── options.go │ ├── options.go │ └── restclientgen.go ├── internal ├── codegen │ ├── common │ │ └── common.go │ ├── es6 │ │ └── es6.go │ ├── golang │ │ ├── golang.go │ │ └── golang_test.go │ ├── grpc │ │ ├── grpc.go │ │ └── grpc_test.go │ └── restclient │ │ ├── golang │ │ └── golang.go │ │ └── restclient.go ├── option │ └── option.go └── stringutil │ └── stringutil.go ├── openapi.go ├── spec └── examples │ ├── v2.0 │ └── petstore-expanded.yaml │ └── v3.0 │ ├── petstore-expanded.yaml │ └── petstore.yaml ├── synopsis_test.go ├── v2 ├── cloner_gen.go ├── contact_accessors_gen.go ├── contact_builder_gen.go ├── contact_json_gen.go ├── contact_mutator_gen.go ├── contact_visitor_gen.go ├── example_map_gen.go ├── extensions_test.go ├── external_documentation_accessors_gen.go ├── external_documentation_builder_gen.go ├── external_documentation_json_gen.go ├── external_documentation_mutator_gen.go ├── external_documentation_visitor_gen.go ├── header.go ├── header_accessors_gen.go ├── header_builder_gen.go ├── header_json_gen.go ├── header_map_gen.go ├── header_mutator_gen.go ├── header_visitor_gen.go ├── info_accessors_gen.go ├── info_builder_gen.go ├── info_json_gen.go ├── info_mutator_gen.go ├── info_visitor_gen.go ├── interface.go ├── interface_gen.go ├── interface_list_gen.go ├── interface_map_gen.go ├── internal │ ├── cmd │ │ └── gentypes │ │ │ └── gentypes.go │ └── types │ │ ├── generate.go │ │ └── interface.go ├── items.go ├── items_accessors_gen.go ├── items_builder_gen.go ├── items_json_gen.go ├── items_mutator_gen.go ├── items_visitor_gen.go ├── iterator_gen.go ├── license_accessors_gen.go ├── license_builder_gen.go ├── license_json_gen.go ├── license_mutator_gen.go ├── license_visitor_gen.go ├── mime_type_list_gen.go ├── openapi.go ├── openapi_test.go ├── operation.go ├── operation_accessors_gen.go ├── operation_builder_gen.go ├── operation_json_gen.go ├── operation_mutator_gen.go ├── operation_visitor_gen.go ├── options.go ├── parameter.go ├── parameter_accessors_gen.go ├── parameter_builder_gen.go ├── parameter_builder_test.go ├── parameter_json_gen.go ├── parameter_list_gen.go ├── parameter_map_gen.go ├── parameter_mutator_gen.go ├── parameter_visitor_gen.go ├── path_item.go ├── path_item_accessors_gen.go ├── path_item_builder.go ├── path_item_builder_gen.go ├── path_item_json_gen.go ├── path_item_map_gen.go ├── path_item_mutator.go ├── path_item_mutator_gen.go ├── path_item_visitor_gen.go ├── paths.go ├── paths_accessors_gen.go ├── paths_builder.go ├── paths_builder_gen.go ├── paths_json.go ├── paths_json_gen.go ├── paths_mutator.go ├── paths_mutator_gen.go ├── paths_visitor_gen.go ├── primitive_types.go ├── resolver.go ├── response.go ├── response_accessors_gen.go ├── response_builder_gen.go ├── response_json_gen.go ├── response_map_gen.go ├── response_mutator_gen.go ├── response_visitor_gen.go ├── responses.go ├── responses_accessors_gen.go ├── responses_builder.go ├── responses_builder_gen.go ├── responses_json_gen.go ├── responses_mutator.go ├── responses_mutator_gen.go ├── responses_visitor_gen.go ├── schema.go ├── schema_accessors_gen.go ├── schema_builder.go ├── schema_builder_gen.go ├── schema_json_gen.go ├── schema_list_gen.go ├── schema_map_gen.go ├── schema_mutator_gen.go ├── schema_visitor_gen.go ├── scheme_list_gen.go ├── scopes_map_gen.go ├── security_requirement.go ├── security_requirement_accessors_gen.go ├── security_requirement_builder_gen.go ├── security_requirement_json_gen.go ├── security_requirement_list_gen.go ├── security_requirement_mutator_gen.go ├── security_requirement_test.go ├── security_requirement_visitor_gen.go ├── security_scheme_accessors_gen.go ├── security_scheme_builder_gen.go ├── security_scheme_json_gen.go ├── security_scheme_map_gen.go ├── security_scheme_mutator_gen.go ├── security_scheme_visitor_gen.go ├── string_list_gen.go ├── string_map_gen.go ├── swagger_accessors_gen.go ├── swagger_builder_gen.go ├── swagger_builder_test.go ├── swagger_json_gen.go ├── swagger_mutator_gen.go ├── swagger_visitor_gen.go ├── tag_accessors_gen.go ├── tag_builder_gen.go ├── tag_json_gen.go ├── tag_list_gen.go ├── tag_mutator_gen.go ├── tag_visitor_gen.go ├── validator.go ├── validator_test.go ├── visitor_gen.go ├── xml_accessors_gen.go ├── xml_builder_gen.go ├── xml_json_gen.go ├── xml_mutator_gen.go └── xml_visitor_gen.go └── v3 ├── callback.go ├── callback_accessors_gen.go ├── callback_builder_gen.go ├── callback_json_gen.go ├── callback_map_gen.go ├── callback_mutator_gen.go ├── callback_visitor_gen.go ├── cloner_gen.go ├── components_accessors_gen.go ├── components_builder.go ├── components_builder_gen.go ├── components_json_gen.go ├── components_mutator_gen.go ├── components_visitor_gen.go ├── contact_accessors_gen.go ├── contact_builder_gen.go ├── contact_json_gen.go ├── contact_mutator_gen.go ├── contact_visitor_gen.go ├── discriminator_accessors_gen.go ├── discriminator_builder_gen.go ├── discriminator_json_gen.go ├── discriminator_mutator_gen.go ├── discriminator_visitor_gen.go ├── encoding.go ├── encoding_accessors_gen.go ├── encoding_builder_gen.go ├── encoding_json_gen.go ├── encoding_map_gen.go ├── encoding_mutator_gen.go ├── encoding_visitor_gen.go ├── entity.go ├── example.go ├── example_accessors_gen.go ├── example_builder_gen.go ├── example_json_gen.go ├── example_map_gen.go ├── example_mutator_gen.go ├── example_visitor_gen.go ├── external_documentation_accessors_gen.go ├── external_documentation_builder_gen.go ├── external_documentation_json_gen.go ├── external_documentation_mutator_gen.go ├── external_documentation_visitor_gen.go ├── header.go ├── header_accessors_gen.go ├── header_builder_gen.go ├── header_json_gen.go ├── header_map_gen.go ├── header_mutator_gen.go ├── header_visitor_gen.go ├── info_accessors_gen.go ├── info_builder_gen.go ├── info_json_gen.go ├── info_mutator_gen.go ├── info_visitor_gen.go ├── interface.go ├── interface_gen.go ├── interface_list_gen.go ├── interface_map_gen.go ├── internal ├── cmd │ └── gentypes │ │ └── gentypes.go └── types │ ├── generate.go │ └── interface.go ├── iterator_gen.go ├── license_accessors_gen.go ├── license_builder_gen.go ├── license_json_gen.go ├── license_mutator_gen.go ├── license_visitor_gen.go ├── link.go ├── link_accessors_gen.go ├── link_builder_gen.go ├── link_json_gen.go ├── link_map_gen.go ├── link_mutator_gen.go ├── link_visitor_gen.go ├── media_type.go ├── media_type_accessors_gen.go ├── media_type_builder_gen.go ├── media_type_json_gen.go ├── media_type_map_gen.go ├── media_type_mutator_gen.go ├── media_type_visitor_gen.go ├── oauth_flow_accessors_gen.go ├── oauth_flow_builder_gen.go ├── oauth_flow_json_gen.go ├── oauth_flow_mutator_gen.go ├── oauth_flow_visitor_gen.go ├── oauth_flows_accessors_gen.go ├── oauth_flows_builder_gen.go ├── oauth_flows_json_gen.go ├── oauth_flows_mutator_gen.go ├── oauth_flows_visitor_gen.go ├── openapi.go ├── openapi_accessors_gen.go ├── openapi_builder_gen.go ├── openapi_example_test.go ├── openapi_json_gen.go ├── openapi_mutator_gen.go ├── openapi_test.go ├── openapi_visitor_gen.go ├── operation.go ├── operation_accessors_gen.go ├── operation_builder.go ├── operation_builder_gen.go ├── operation_json_gen.go ├── operation_mutator_gen.go ├── operation_test.go ├── operation_visitor_gen.go ├── options.go ├── parameter.go ├── parameter_accessors_gen.go ├── parameter_builder_gen.go ├── parameter_json_gen.go ├── parameter_list_gen.go ├── parameter_map_gen.go ├── parameter_mutator_gen.go ├── parameter_visitor_gen.go ├── path_item.go ├── path_item_accessors_gen.go ├── path_item_builder.go ├── path_item_builder_gen.go ├── path_item_json_gen.go ├── path_item_map_gen.go ├── path_item_mutator.go ├── path_item_mutator_gen.go ├── path_item_visitor_gen.go ├── paths.go ├── paths_accessors_gen.go ├── paths_builder.go ├── paths_builder_gen.go ├── paths_json_gen.go ├── paths_mutator_gen.go ├── paths_visitor_gen.go ├── request_body.go ├── request_body_accessors_gen.go ├── request_body_builder.go ├── request_body_builder_gen.go ├── request_body_json_gen.go ├── request_body_map_gen.go ├── request_body_mutator_gen.go ├── request_body_visitor_gen.go ├── resolver.go ├── response.go ├── response_accessors_gen.go ├── response_builder.go ├── response_builder_gen.go ├── response_json_gen.go ├── response_map_gen.go ├── response_mutator_gen.go ├── response_visitor_gen.go ├── responses.go ├── responses_accessors_gen.go ├── responses_builder.go ├── responses_builder_gen.go ├── responses_json_gen.go ├── responses_mutator_gen.go ├── responses_visitor_gen.go ├── schema.go ├── schema_accessors_gen.go ├── schema_builder.go ├── schema_builder_gen.go ├── schema_json_gen.go ├── schema_list_gen.go ├── schema_map_gen.go ├── schema_mutator_gen.go ├── schema_visitor_gen.go ├── scope_map_gen.go ├── security_requirement_accessors_gen.go ├── security_requirement_builder_gen.go ├── security_requirement_json_gen.go ├── security_requirement_list_gen.go ├── security_requirement_mutator_gen.go ├── security_requirement_visitor_gen.go ├── security_scheme_accessors_gen.go ├── security_scheme_builder_gen.go ├── security_scheme_json_gen.go ├── security_scheme_map_gen.go ├── security_scheme_mutator_gen.go ├── security_scheme_visitor_gen.go ├── server_accessors_gen.go ├── server_builder_gen.go ├── server_json_gen.go ├── server_list_gen.go ├── server_mutator_gen.go ├── server_variable.go ├── server_variable_accessors_gen.go ├── server_variable_builder_gen.go ├── server_variable_json_gen.go ├── server_variable_map_gen.go ├── server_variable_mutator_gen.go ├── server_variable_visitor_gen.go ├── server_visitor_gen.go ├── string_list_gen.go ├── string_list_map_gen.go ├── string_map_gen.go ├── tag_accessors_gen.go ├── tag_builder_gen.go ├── tag_json_gen.go ├── tag_list_gen.go ├── tag_mutator_gen.go ├── tag_visitor_gen.go ├── validator.go └── visitor_gen.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | sudo: false 3 | go: 4 | - "1.10" 5 | - tip 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /generator/grpcgen/options.go: -------------------------------------------------------------------------------- 1 | package grpcgen 2 | 3 | import ( 4 | "io" 5 | 6 | "github.com/lestrrat-go/openapi/internal/option" 7 | ) 8 | 9 | func WithDestination(dst io.Writer) Option { 10 | return option.New(optkeyDestination, dst) 11 | } 12 | 13 | func WithPackageName(name string) Option { 14 | return option.New(optkeyPackageName, name) 15 | } 16 | 17 | func WithAnnotation(b bool) Option { 18 | return option.New(optkeyAnnotation, b) 19 | } 20 | 21 | func WithGlobalOption(key, value string) Option { 22 | return option.New(optkeyGlobalOption, &globalOption{name: key, value: value}) 23 | } 24 | -------------------------------------------------------------------------------- /generator/restclientgen/compiler/call.go: -------------------------------------------------------------------------------- 1 | package compiler 2 | 3 | func (call *Call) SecuritySettings() []*SecuritySettings { 4 | return call.securitySettings 5 | } 6 | 7 | func (call *Call) Name() string { 8 | return call.name 9 | } 10 | 11 | func (call *Call) Responses() []*Response { 12 | return call.responses 13 | } 14 | 15 | func (call *Call) Verb() string { 16 | return call.verb 17 | } 18 | 19 | func (call *Call) Method() string { 20 | return call.method 21 | } 22 | 23 | func (call *Call) Body() Type { 24 | return call.body 25 | } 26 | 27 | func (call *Call) Query() Type { 28 | return call.query 29 | } 30 | 31 | func (call *Call) Header() Type { 32 | return call.header 33 | } 34 | 35 | func (call *Call) Path() Type { 36 | return call.path 37 | } 38 | 39 | func (call *Call) RequestPath() string { 40 | return call.requestPath 41 | } 42 | 43 | func (call *Call) Optionals() []*Field { 44 | return call.optionals 45 | } 46 | func (call *Call) Requireds() []*Field { 47 | return call.requireds 48 | } 49 | 50 | func (call *Call) AllFields() []*Field { 51 | return call.allFields 52 | } 53 | 54 | func (call *Call) DefaultConsumes() string { 55 | // default to "application/x-www-form-urlencoded" 56 | if len(call.consumes) == 0 { 57 | return "application/x-www-form-urlencoded" 58 | } 59 | return call.consumes[0] 60 | } 61 | 62 | func (call *Call) Consumes() []string { 63 | return call.consumes 64 | } 65 | 66 | -------------------------------------------------------------------------------- /generator/restclientgen/es6flow/options.go: -------------------------------------------------------------------------------- 1 | package es6flow 2 | 3 | import "github.com/lestrrat-go/openapi/internal/option" 4 | 5 | type Option = option.Interface 6 | 7 | const ( 8 | optkeyClientName = "clientName" 9 | optkeyDefaultServiceName = "serviceName" 10 | optkeyDirectory = "directory" 11 | ) 12 | 13 | func WithDir(s string) Option { 14 | return option.New(optkeyDirectory, s) 15 | } 16 | 17 | func WithDefaultServiceName(s string) Option { 18 | return option.New(optkeyDefaultServiceName, s) 19 | } 20 | -------------------------------------------------------------------------------- /generator/restclientgen/golang/interface.go: -------------------------------------------------------------------------------- 1 | package golang 2 | 3 | import ( 4 | "github.com/lestrrat-go/openapi/generator/restclientgen/compiler" 5 | ) 6 | 7 | type Context struct { 8 | client *compiler.ClientDefinition 9 | dir string 10 | packageName string 11 | exportNew bool 12 | } 13 | 14 | type Type interface { 15 | Name() string 16 | SetName(string) 17 | } 18 | 19 | type Builtin string 20 | 21 | func (b Builtin) Name() string { 22 | return string(b) 23 | } 24 | 25 | func (b Builtin) SetName(s string) { 26 | panic("oops?") 27 | } 28 | 29 | type Array struct { 30 | name string 31 | elem string 32 | } 33 | 34 | func (a *Array) SetName(s string) { 35 | a.name = s 36 | } 37 | 38 | func isBuiltinType(s string) bool { 39 | switch s { 40 | case "string", 41 | "int", "int8", "int16", "int32", "int64", 42 | "uint", "uint8", "uint16", "uint32", "uint64", 43 | "float32", "float64", "byte", "rune", "bool": 44 | return true 45 | default: 46 | return false 47 | } 48 | } 49 | 50 | func (a *Array) Name() string { 51 | if a.name == "" { 52 | if isBuiltinType(a.elem) { 53 | return "[]" + a.elem 54 | } else { 55 | return "[]*" + a.elem 56 | } 57 | } 58 | return a.name 59 | } 60 | -------------------------------------------------------------------------------- /generator/restclientgen/golang/options.go: -------------------------------------------------------------------------------- 1 | package golang 2 | 3 | import "github.com/lestrrat-go/openapi/internal/option" 4 | 5 | type Option = option.Interface 6 | 7 | const ( 8 | optkeyDefaultServiceName = "serviceName" 9 | optkeyDirectory = "directory" 10 | optkeyExportNew = "exportNew" 11 | optkeyPackageName = "packageName" 12 | ) 13 | 14 | func WithDir(s string) Option { 15 | return option.New(optkeyDirectory, s) 16 | } 17 | 18 | func WithPackageName(s string) Option { 19 | return option.New(optkeyPackageName, s) 20 | } 21 | 22 | func WithDefaultServiceName(s string) Option { 23 | return option.New(optkeyDefaultServiceName, s) 24 | } 25 | 26 | func WithExportNew(v bool) Option { 27 | return option.New(optkeyExportNew, v) 28 | } 29 | 30 | -------------------------------------------------------------------------------- /generator/restclientgen/options.go: -------------------------------------------------------------------------------- 1 | package restclientgen 2 | 3 | import "github.com/lestrrat-go/openapi/internal/option" 4 | 5 | type Option = option.Interface 6 | 7 | const ( 8 | optkeyClientName = "clientName" 9 | optkeyDefaultServiceName = "serviceName" 10 | optkeyDirectory = "directory" 11 | optkeyExportNew = "exportNew" 12 | optkeyPackageName = "packageName" 13 | optkeyTarget = "target" 14 | ) 15 | 16 | func WithClientName(s string) Option { 17 | return option.New(optkeyClientName, s) 18 | } 19 | 20 | func WithDir(s string) Option { 21 | return option.New(optkeyDirectory, s) 22 | } 23 | 24 | func WithPackageName(s string) Option { 25 | return option.New(optkeyPackageName, s) 26 | } 27 | 28 | func WithDefaultServiceName(s string) Option { 29 | return option.New(optkeyDefaultServiceName, s) 30 | } 31 | 32 | func WithExportNew(v bool) Option { 33 | return option.New(optkeyExportNew, v) 34 | } 35 | 36 | func WithTarget(s string) Option { 37 | return option.New(optkeyTarget, s) 38 | } 39 | -------------------------------------------------------------------------------- /generator/restclientgen/restclientgen.go: -------------------------------------------------------------------------------- 1 | package restclientgen 2 | 3 | import ( 4 | "github.com/lestrrat-go/openapi/generator/restclientgen/es6flow" 5 | "github.com/lestrrat-go/openapi/generator/restclientgen/golang" 6 | openapi "github.com/lestrrat-go/openapi/v2" 7 | "github.com/pkg/errors" 8 | ) 9 | 10 | func Generate(spec openapi.Swagger, options ...Option) error { 11 | if err := spec.Validate(true); err != nil { 12 | return errors.Wrap(err, `failed to validate spec`) 13 | } 14 | 15 | target := "go" 16 | for _, option := range options { 17 | switch option.Name() { 18 | case optkeyTarget: 19 | target = option.Value().(string) 20 | } 21 | } 22 | 23 | switch target { 24 | case "go": 25 | return golang.Generate(spec, options...) 26 | case "es6flow": 27 | return es6flow.Generate(spec, options...) 28 | default: 29 | return errors.Errorf(`invalid generation target %s`, target) 30 | } 31 | return errors.New(`unreachable`) 32 | } 33 | -------------------------------------------------------------------------------- /internal/codegen/golang/golang_test.go: -------------------------------------------------------------------------------- 1 | package golang_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/lestrrat-go/openapi/internal/codegen/golang" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func TestExportedName(t *testing.T) { 11 | var tests = []struct { 12 | Expected string 13 | Input string 14 | }{ 15 | {Expected: "FooXMLBlah", Input: "fooXMLBlah"}, 16 | } 17 | 18 | for _, test := range tests { 19 | t.Run(test.Expected, func(t *testing.T) { 20 | if !assert.Equal(t, test.Expected, golang.ExportedName(test.Input), `names should match`) { 21 | return 22 | } 23 | }) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /internal/codegen/grpc/grpc.go: -------------------------------------------------------------------------------- 1 | package grpc 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/lestrrat-go/openapi/internal/codegen/golang" 7 | ) 8 | 9 | var fieldNameReplacer = strings.NewReplacer( 10 | "-", "_", 11 | " ", "_", 12 | ) 13 | 14 | func FieldName(s string) string { 15 | // All non-alphanumeric characters except for "_" are 16 | // converted to "_" 17 | // (note: too lazy to make list right now... 18 | return fieldNameReplacer.Replace(s) 19 | } 20 | 21 | func MessageName(s string) string { 22 | return golang.ExportedName(s) 23 | } 24 | -------------------------------------------------------------------------------- /internal/codegen/grpc/grpc_test.go: -------------------------------------------------------------------------------- 1 | package grpc_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/lestrrat-go/openapi/internal/codegen/grpc" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func TestFieldName(t *testing.T) { 11 | var data = []struct { 12 | Input string 13 | Expected string 14 | }{ 15 | { 16 | Input: "foo-bar-baz", 17 | Expected: "foo_bar_baz", 18 | }, 19 | } 20 | 21 | for _, c := range data { 22 | if !assert.Equal(t, c.Expected, grpc.FieldName(c.Input), "expected %s", c.Expected) { 23 | return 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /internal/codegen/restclient/golang/golang.go: -------------------------------------------------------------------------------- 1 | package golang 2 | 3 | import ( 4 | "github.com/lestrrat-go/openapi/internal/codegen/golang" 5 | "github.com/lestrrat-go/openapi/internal/codegen/restclient" 6 | openapi "github.com/lestrrat-go/openapi/v2" 7 | ) 8 | 9 | func CallObjectName(oper openapi.Operation) string { 10 | return golang.ExportedName(restclient.CallObjectName(oper)) 11 | } 12 | 13 | func CallMethodName(oper openapi.Operation) string { 14 | if name := restclient.CallMethodName(oper); name != "" { 15 | return golang.ExportedName(name) 16 | } 17 | return "" 18 | } 19 | -------------------------------------------------------------------------------- /internal/codegen/restclient/restclient.go: -------------------------------------------------------------------------------- 1 | package restclient 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "strings" 7 | 8 | "github.com/ghodss/yaml" 9 | openapi "github.com/lestrrat-go/openapi/v2" 10 | ) 11 | 12 | // CallObjectName generates a Call object type name. It is NOT 13 | // normalized, so the caller needs to normalize it 14 | func CallObjectName(oper openapi.Operation) string { 15 | if operID := oper.OperationID(); operID != "" { 16 | return operID + "_Call" 17 | } 18 | 19 | pi := oper.PathItem() 20 | if pi == nil { 21 | buf, err := yaml.Marshal(oper) 22 | if err != nil { 23 | fmt.Fprintf(os.Stdout, err.Error()) 24 | } else { 25 | os.Stdout.Write(buf) 26 | } 27 | panic("PathItem for operation is nil") 28 | } 29 | 30 | verb := strings.ToLower(oper.Verb()) 31 | return verb + "_" + oper.PathItem().Path() + "_Call" 32 | } 33 | 34 | func CallMethodName(oper openapi.Operation) string { 35 | rawMethodName, ok := oper.Extension(`x-call-method-name`) 36 | if !ok { 37 | return "" 38 | } 39 | 40 | if s, ok := rawMethodName.(string); ok { 41 | return s 42 | } 43 | return "" 44 | } 45 | -------------------------------------------------------------------------------- /internal/option/option.go: -------------------------------------------------------------------------------- 1 | package option 2 | 3 | type Interface interface { 4 | Name() string 5 | Value() interface{} 6 | } 7 | 8 | type Option struct { 9 | name string 10 | value interface{} 11 | } 12 | 13 | func New(name string, value interface{}) *Option { 14 | return &Option{ 15 | name: name, 16 | value: value, 17 | } 18 | } 19 | 20 | func (o *Option) Name() string { 21 | return o.name 22 | } 23 | func (o *Option) Value() interface{} { 24 | return o.value 25 | } 26 | -------------------------------------------------------------------------------- /openapi.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | -------------------------------------------------------------------------------- /synopsis_test.go: -------------------------------------------------------------------------------- 1 | package openapi_test 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | v2 "github.com/lestrrat-go/openapi/v2" 8 | ) 9 | 10 | func ExampleV2() { 11 | const src = `spec/examples/v2.0/petstore-extended.yaml` 12 | f, err := os.Open(src) 13 | if err != nil { 14 | log.Printf(`failed to open file %s: %s`, src, err) 15 | return 16 | } 17 | 18 | spec, err := v2.ParseYAML(f) 19 | if err != nil { 20 | log.Printf(`failed to parse file %s: %s`, src, err) 21 | return 22 | } 23 | 24 | for pathItemIter := spec.Paths().Paths(); pathItemIter.Next(); { 25 | path, pathItem := pathItemIter.Item() 26 | for operIter := pathItem.Operations(); operIter.Next(); { 27 | oper := operIter.Item() 28 | log.Printf("%s %s", oper.Verb(), path) 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /v2/contact_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "sort" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = sort.Strings 15 | var _ = errors.Cause 16 | 17 | func (v *contact) IsValid() bool { 18 | return v != nil 19 | } 20 | 21 | func (v *contact) Name() string { 22 | return v.name 23 | } 24 | 25 | func (v *contact) URL() string { 26 | return v.url 27 | } 28 | 29 | func (v *contact) Email() string { 30 | return v.email 31 | } 32 | 33 | // Reference returns the value of `$ref` field 34 | func (v *contact) Reference() string { 35 | return v.reference 36 | } 37 | 38 | func (v *contact) IsUnresolved() bool { 39 | return v.reference != "" && !v.resolved 40 | } 41 | 42 | // Extension returns the value of an arbitrary extension 43 | func (v *contact) Extension(key string) (interface{}, bool) { 44 | e, ok := v.extensions[key] 45 | return e, ok 46 | } 47 | 48 | // Extensions return an iterator to iterate over all extensions 49 | func (v *contact) Extensions() *ExtensionsIterator { 50 | var items []interface{} 51 | for key, item := range v.extensions { 52 | items = append(items, &mapIteratorItem{key: key, item: item}) 53 | } 54 | var iter ExtensionsIterator 55 | iter.list.size = len(items) 56 | iter.list.items = items 57 | return &iter 58 | } 59 | 60 | func (v *contact) Validate(recurse bool) error { 61 | return newValidator(recurse).Validate(context.Background(), v) 62 | } 63 | -------------------------------------------------------------------------------- /v2/contact_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ContactVisitor is an interface for objects that knows 16 | // how to process Contact elements while traversing the OpenAPI structure 17 | type ContactVisitor interface { 18 | VisitContact(context.Context, Contact) error 19 | } 20 | 21 | func visitContact(ctx context.Context, elem Contact) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(contactVisitorCtxKey{}).(ContactVisitor); ok { 34 | if err := v.VisitContact(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit Contact element`) 39 | } 40 | } 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /v2/example_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from ExampleMap 18 | func (v *ExampleMap) Clear() error { 19 | *v = make(ExampleMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in ExampleMap 24 | func (v *ExampleMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v ExampleMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if qj, ok := target.(QueryJSONer); ok { 43 | return qj.QueryJSON(path) 44 | } 45 | 46 | if path == `` { 47 | return target, true 48 | } 49 | return nil, false 50 | } 51 | -------------------------------------------------------------------------------- /v2/extensions_test.go: -------------------------------------------------------------------------------- 1 | package openapi_test 2 | 3 | import ( 4 | "testing" 5 | 6 | openapi "github.com/lestrrat-go/openapi/v2" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func TestExtensions(t *testing.T) { 11 | contact := apiSupport.Clone() 12 | err := openapi.MutateContact(contact). 13 | Extension(`x-foo`, `Foo`). 14 | Extension(`x-bar`, `Bar`). 15 | Apply() 16 | if !assert.NoError(t, err, `mutating contact should succeed`) { 17 | return 18 | } 19 | 20 | for iter := contact.Extensions(); iter.Next(); { 21 | key, value := iter.Item() 22 | t.Logf("%#v %#v", key, value) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /v2/external_documentation_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "sort" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = sort.Strings 15 | var _ = errors.Cause 16 | 17 | func (v *externalDocumentation) IsValid() bool { 18 | return v != nil 19 | } 20 | 21 | func (v *externalDocumentation) URL() string { 22 | return v.url 23 | } 24 | 25 | func (v *externalDocumentation) Description() string { 26 | return v.description 27 | } 28 | 29 | // Reference returns the value of `$ref` field 30 | func (v *externalDocumentation) Reference() string { 31 | return v.reference 32 | } 33 | 34 | func (v *externalDocumentation) IsUnresolved() bool { 35 | return v.reference != "" && !v.resolved 36 | } 37 | 38 | // Extension returns the value of an arbitrary extension 39 | func (v *externalDocumentation) Extension(key string) (interface{}, bool) { 40 | e, ok := v.extensions[key] 41 | return e, ok 42 | } 43 | 44 | // Extensions return an iterator to iterate over all extensions 45 | func (v *externalDocumentation) Extensions() *ExtensionsIterator { 46 | var items []interface{} 47 | for key, item := range v.extensions { 48 | items = append(items, &mapIteratorItem{key: key, item: item}) 49 | } 50 | var iter ExtensionsIterator 51 | iter.list.size = len(items) 52 | iter.list.items = items 53 | return &iter 54 | } 55 | 56 | func (v *externalDocumentation) Validate(recurse bool) error { 57 | return newValidator(recurse).Validate(context.Background(), v) 58 | } 59 | -------------------------------------------------------------------------------- /v2/external_documentation_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ExternalDocumentationVisitor is an interface for objects that knows 16 | // how to process ExternalDocumentation elements while traversing the OpenAPI structure 17 | type ExternalDocumentationVisitor interface { 18 | VisitExternalDocumentation(context.Context, ExternalDocumentation) error 19 | } 20 | 21 | func visitExternalDocumentation(ctx context.Context, elem ExternalDocumentation) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(externalDocumentationVisitorCtxKey{}).(ExternalDocumentationVisitor); ok { 34 | if err := v.VisitExternalDocumentation(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit ExternalDocumentation element`) 39 | } 40 | } 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /v2/header.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *header) setName(s string) { 4 | v.name = s 5 | } -------------------------------------------------------------------------------- /v2/header_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from HeaderMap 18 | func (v *HeaderMap) Clear() error { 19 | *v = make(HeaderMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in HeaderMap 24 | func (v *HeaderMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v HeaderMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if qj, ok := target.(QueryJSONer); ok { 43 | return qj.QueryJSON(path) 44 | } 45 | 46 | if path == `` { 47 | return target, true 48 | } 49 | return nil, false 50 | } 51 | 52 | // UnmarshalJSON takes a JSON buffer and properly populates `v` 53 | func (v *HeaderMap) UnmarshalJSON(data []byte) error { 54 | var proxy map[string]*header 55 | if err := json.Unmarshal(data, &proxy); err != nil { 56 | return errors.Wrap(err, `failed to unmarshal`) 57 | } 58 | tmp := make(map[string]Header) 59 | for name, value := range proxy { 60 | value.setName(name) 61 | tmp[name] = value 62 | } 63 | *v = tmp 64 | return nil 65 | } 66 | -------------------------------------------------------------------------------- /v2/header_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // HeaderVisitor is an interface for objects that knows 16 | // how to process Header elements while traversing the OpenAPI structure 17 | type HeaderVisitor interface { 18 | VisitHeader(context.Context, Header) error 19 | } 20 | 21 | func visitHeader(ctx context.Context, elem Header) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(headerVisitorCtxKey{}).(HeaderVisitor); ok { 34 | if err := v.VisitHeader(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit Header element`) 39 | } 40 | } 41 | 42 | if child := elem.Items(); child != nil { 43 | if err := visitItems(ctx, child); err != nil { 44 | return errors.Wrap(err, `failed to visit Items element for Header`) 45 | } 46 | } 47 | return nil 48 | } 49 | -------------------------------------------------------------------------------- /v2/info_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "sort" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = sort.Strings 15 | var _ = errors.Cause 16 | 17 | func (v *info) IsValid() bool { 18 | return v != nil 19 | } 20 | 21 | func (v *info) Title() string { 22 | return v.title 23 | } 24 | 25 | func (v *info) Version() string { 26 | return v.version 27 | } 28 | 29 | func (v *info) Description() string { 30 | return v.description 31 | } 32 | 33 | func (v *info) TermsOfService() string { 34 | return v.termsOfService 35 | } 36 | 37 | func (v *info) Contact() Contact { 38 | return v.contact 39 | } 40 | 41 | func (v *info) License() License { 42 | return v.license 43 | } 44 | 45 | // Reference returns the value of `$ref` field 46 | func (v *info) Reference() string { 47 | return v.reference 48 | } 49 | 50 | func (v *info) IsUnresolved() bool { 51 | return v.reference != "" && !v.resolved 52 | } 53 | 54 | // Extension returns the value of an arbitrary extension 55 | func (v *info) Extension(key string) (interface{}, bool) { 56 | e, ok := v.extensions[key] 57 | return e, ok 58 | } 59 | 60 | // Extensions return an iterator to iterate over all extensions 61 | func (v *info) Extensions() *ExtensionsIterator { 62 | var items []interface{} 63 | for key, item := range v.extensions { 64 | items = append(items, &mapIteratorItem{key: key, item: item}) 65 | } 66 | var iter ExtensionsIterator 67 | iter.list.size = len(items) 68 | iter.list.items = items 69 | return &iter 70 | } 71 | 72 | func (v *info) Validate(recurse bool) error { 73 | return newValidator(recurse).Validate(context.Background(), v) 74 | } 75 | -------------------------------------------------------------------------------- /v2/info_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // InfoVisitor is an interface for objects that knows 16 | // how to process Info elements while traversing the OpenAPI structure 17 | type InfoVisitor interface { 18 | VisitInfo(context.Context, Info) error 19 | } 20 | 21 | func visitInfo(ctx context.Context, elem Info) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(infoVisitorCtxKey{}).(InfoVisitor); ok { 34 | if err := v.VisitInfo(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit Info element`) 39 | } 40 | } 41 | 42 | if child := elem.Contact(); child != nil { 43 | if err := visitContact(ctx, child); err != nil { 44 | return errors.Wrap(err, `failed to visit Contact element for Info`) 45 | } 46 | } 47 | 48 | if child := elem.License(); child != nil { 49 | if err := visitLicense(ctx, child); err != nil { 50 | return errors.Wrap(err, `failed to visit License element for Info`) 51 | } 52 | } 53 | return nil 54 | } 55 | -------------------------------------------------------------------------------- /v2/interface.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | type MapQueryJSON map[string]interface{} 4 | type SliceQueryJSON []interface{} 5 | 6 | type QueryJSONer interface { 7 | QueryJSON(string) (interface{}, bool) 8 | } 9 | 10 | type nilLock struct{} 11 | func (l nilLock) Lock() {} 12 | func (l nilLock) Unlock() {} 13 | 14 | const ( 15 | Int32 = "int32" 16 | Int64 = "int64" 17 | Float = "float" 18 | Double = "double" 19 | ) 20 | -------------------------------------------------------------------------------- /v2/interface_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all values from InterfaceList 18 | func (v *InterfaceList) Clear() error { 19 | *v = InterfaceList(nil) 20 | return nil 21 | } 22 | 23 | // Validate checks for the values for correctness. If `recurse` 24 | // is specified, child elements are also validated 25 | func (v *InterfaceList) Validate(recurse bool) error { 26 | for i, elem := range *v { 27 | if validator, ok := elem.(Validator); ok { 28 | if err := validator.Validate(recurse); err != nil { 29 | return errors.Wrapf(err, `failed to validate element %d`, i) 30 | } 31 | } 32 | } 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /v2/interface_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from InterfaceMap 18 | func (v *InterfaceMap) Clear() error { 19 | *v = make(InterfaceMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in InterfaceMap 24 | func (v *InterfaceMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v InterfaceMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if qj, ok := target.(QueryJSONer); ok { 43 | return qj.QueryJSON(path) 44 | } 45 | 46 | if path == `` { 47 | return target, true 48 | } 49 | return nil, false 50 | } 51 | -------------------------------------------------------------------------------- /v2/internal/cmd/gentypes/gentypes.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/lestrrat-go/openapi/v2/internal/types" 8 | ) 9 | 10 | func main() { 11 | if err := _main(); err != nil { 12 | log.Printf("%s", err) 13 | os.Exit(1) 14 | } 15 | } 16 | 17 | func _main() error { 18 | return types.GenerateCode() 19 | } 20 | -------------------------------------------------------------------------------- /v2/items.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import "github.com/pkg/errors" 4 | 5 | func (v *items) ConvertToSchema() (Schema, error) { 6 | b := NewSchema(). 7 | Type(v.Type()). 8 | Format(v.Format()). 9 | Default(v.Default()). 10 | Pattern(v.Pattern()). 11 | Reference(v.Reference()). 12 | UniqueItems(v.UniqueItems()) 13 | 14 | if items := v.Items(); items != nil { 15 | s, err := items.ConvertToSchema() 16 | if err != nil { 17 | return nil, errors.Wrap(err, `failed to convert items to schema`) 18 | } 19 | b.Items(s) 20 | } 21 | 22 | if v.HasMaximum() { 23 | b.Maximum(v.Maximum()) 24 | } 25 | 26 | if v.HasExclusiveMaximum() { 27 | b.ExclusiveMaximum(v.ExclusiveMaximum()) 28 | } 29 | 30 | if v.HasMinimum() { 31 | b.Minimum(v.Minimum()) 32 | } 33 | 34 | if v.HasExclusiveMinimum() { 35 | b.ExclusiveMinimum(v.ExclusiveMinimum()) 36 | } 37 | 38 | if v.HasMaxLength() { 39 | b.MaxLength(v.MaxLength()) 40 | } 41 | 42 | if v.HasMinLength() { 43 | b.MinLength(v.MinLength()) 44 | } 45 | 46 | if v.HasMaxItems() { 47 | b.MaxItems(v.MaxItems()) 48 | } 49 | 50 | if v.HasMinLength() { 51 | b.MinLength(v.MinLength()) 52 | } 53 | 54 | if v.HasMinItems() { 55 | b.MinItems(v.MinItems()) 56 | } 57 | 58 | if v.HasMultipleOf() { 59 | b.MultipleOf(v.MultipleOf()) 60 | } 61 | 62 | for iter := v.Enum(); iter.Next(); { 63 | b.Enum(iter.Item()) 64 | } 65 | 66 | for iter := v.Extensions(); iter.Next(); { 67 | b.Extension(iter.Item()) 68 | } 69 | return b.Build() 70 | } 71 | -------------------------------------------------------------------------------- /v2/items_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ItemsVisitor is an interface for objects that knows 16 | // how to process Items elements while traversing the OpenAPI structure 17 | type ItemsVisitor interface { 18 | VisitItems(context.Context, Items) error 19 | } 20 | 21 | func visitItems(ctx context.Context, elem Items) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(itemsVisitorCtxKey{}).(ItemsVisitor); ok { 34 | if err := v.VisitItems(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit Items element`) 39 | } 40 | } 41 | 42 | if child := elem.Items(); child != nil { 43 | if err := visitItems(ctx, child); err != nil { 44 | return errors.Wrap(err, `failed to visit Items element for Items`) 45 | } 46 | } 47 | return nil 48 | } 49 | -------------------------------------------------------------------------------- /v2/license_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "sort" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = sort.Strings 15 | var _ = errors.Cause 16 | 17 | func (v *license) IsValid() bool { 18 | return v != nil 19 | } 20 | 21 | func (v *license) Name() string { 22 | return v.name 23 | } 24 | 25 | func (v *license) URL() string { 26 | return v.url 27 | } 28 | 29 | // Reference returns the value of `$ref` field 30 | func (v *license) Reference() string { 31 | return v.reference 32 | } 33 | 34 | func (v *license) IsUnresolved() bool { 35 | return v.reference != "" && !v.resolved 36 | } 37 | 38 | // Extension returns the value of an arbitrary extension 39 | func (v *license) Extension(key string) (interface{}, bool) { 40 | e, ok := v.extensions[key] 41 | return e, ok 42 | } 43 | 44 | // Extensions return an iterator to iterate over all extensions 45 | func (v *license) Extensions() *ExtensionsIterator { 46 | var items []interface{} 47 | for key, item := range v.extensions { 48 | items = append(items, &mapIteratorItem{key: key, item: item}) 49 | } 50 | var iter ExtensionsIterator 51 | iter.list.size = len(items) 52 | iter.list.items = items 53 | return &iter 54 | } 55 | 56 | func (v *license) Validate(recurse bool) error { 57 | return newValidator(recurse).Validate(context.Background(), v) 58 | } 59 | -------------------------------------------------------------------------------- /v2/license_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // LicenseVisitor is an interface for objects that knows 16 | // how to process License elements while traversing the OpenAPI structure 17 | type LicenseVisitor interface { 18 | VisitLicense(context.Context, License) error 19 | } 20 | 21 | func visitLicense(ctx context.Context, elem License) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(licenseVisitorCtxKey{}).(LicenseVisitor); ok { 34 | if err := v.VisitLicense(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit License element`) 39 | } 40 | } 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /v2/mime_type_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all values from MIMETypeList 18 | func (v *MIMETypeList) Clear() error { 19 | *v = MIMETypeList(nil) 20 | return nil 21 | } 22 | 23 | // Validate checks for the values for correctness. If `recurse` 24 | // is specified, child elements are also validated 25 | func (v *MIMETypeList) Validate(recurse bool) error { 26 | return nil 27 | } 28 | -------------------------------------------------------------------------------- /v2/operation.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *operation) setPathItem(pi PathItem) { 4 | v.pathItem = pi 5 | } 6 | 7 | func (v *operation) setVerb(s string) { 8 | v.verb = s 9 | } 10 | -------------------------------------------------------------------------------- /v2/options.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import ( 4 | "sync" 5 | 6 | "github.com/lestrrat-go/openapi/internal/option" 7 | ) 8 | 9 | const ( 10 | optkeyLocker = "locker" 11 | optkeyValidate = "validate" 12 | ) 13 | 14 | type Option = option.Interface 15 | 16 | // WithValidate specifies if validation should be performed on the 17 | // object. This option can be passed to `ParseYAML`, `ParseJSON`, 18 | // and `Do` methods for builders and mutators. 19 | func WithValidate(v bool) Option { 20 | return option.New(optkeyValidate, v) 21 | } 22 | 23 | // WithLocker specifies the lock object to be used for applicable 24 | // constructs, such as a Builder object. If a nil value is passed, 25 | // usually it effectively becomes a non-locking operation. Please 26 | // Consult the specific construct you're working with for exact 27 | // semantics 28 | func WithLocker(v sync.Locker) Option { 29 | return option.New(optkeyLocker, v) 30 | } 31 | -------------------------------------------------------------------------------- /v2/parameter.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import "github.com/pkg/errors" 4 | 5 | func (v *parameter) ConvertToSchema() (Schema, error) { 6 | if v.in == InBody { 7 | return v.schema.Clone(), nil 8 | } 9 | 10 | b := NewSchema(). 11 | Type(v.Type()). 12 | Format(v.Format()). 13 | Default(v.Default()). 14 | Pattern(v.Pattern()). 15 | Reference(v.Reference()). 16 | UniqueItems(v.UniqueItems()) 17 | 18 | if items := v.Items(); items != nil { 19 | s, err := items.ConvertToSchema() 20 | if err != nil { 21 | return nil, errors.Wrap(err, `failed to convert items to schema`) 22 | } 23 | b.Items(s) 24 | } 25 | 26 | if v.HasMaximum() { 27 | b.Maximum(v.Maximum()) 28 | } 29 | 30 | if v.HasExclusiveMaximum() { 31 | b.ExclusiveMaximum(v.ExclusiveMaximum()) 32 | } 33 | 34 | if v.HasMinimum() { 35 | b.Minimum(v.Minimum()) 36 | } 37 | 38 | if v.HasExclusiveMinimum() { 39 | b.ExclusiveMinimum(v.ExclusiveMinimum()) 40 | } 41 | 42 | if v.HasMaxLength() { 43 | b.MaxLength(v.MaxLength()) 44 | } 45 | 46 | if v.HasMinLength() { 47 | b.MinLength(v.MinLength()) 48 | } 49 | 50 | if v.HasMaxItems() { 51 | b.MaxItems(v.MaxItems()) 52 | } 53 | 54 | if v.HasMinLength() { 55 | b.MinLength(v.MinLength()) 56 | } 57 | 58 | if v.HasMinItems() { 59 | b.MinItems(v.MinItems()) 60 | } 61 | 62 | if v.HasMultipleOf() { 63 | b.MultipleOf(v.MultipleOf()) 64 | } 65 | 66 | for iter := v.Enum(); iter.Next(); { 67 | b.Enum(iter.Item()) 68 | } 69 | 70 | for iter := v.Extensions(); iter.Next(); { 71 | b.Extension(iter.Item()) 72 | } 73 | return b.Build() 74 | } 75 | -------------------------------------------------------------------------------- /v2/parameter_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all values from ParameterList 18 | func (v *ParameterList) Clear() error { 19 | *v = ParameterList(nil) 20 | return nil 21 | } 22 | 23 | // Validate checks for the values for correctness. If `recurse` 24 | // is specified, child elements are also validated 25 | func (v *ParameterList) Validate(recurse bool) error { 26 | for i, elem := range *v { 27 | if validator, ok := elem.(Validator); ok { 28 | if err := validator.Validate(recurse); err != nil { 29 | return errors.Wrapf(err, `failed to validate element %d`, i) 30 | } 31 | } 32 | } 33 | return nil 34 | } 35 | 36 | // UnmarshalJSON defines how ParameterList is deserialized from JSON 37 | func (v *ParameterList) UnmarshalJSON(data []byte) error { 38 | var proxy []*parameter 39 | if err := json.Unmarshal(data, &proxy); err != nil { 40 | return errors.Wrap(err, `failed to unmarshal`) 41 | } 42 | 43 | if len(proxy) == 0 { 44 | *v = ParameterList(nil) 45 | return nil 46 | } 47 | 48 | tmp := make(ParameterList, len(proxy)) 49 | for i, value := range proxy { 50 | tmp[i] = value 51 | } 52 | *v = tmp 53 | return nil 54 | } 55 | -------------------------------------------------------------------------------- /v2/parameter_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from ParameterMap 18 | func (v *ParameterMap) Clear() error { 19 | *v = make(ParameterMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in ParameterMap 24 | func (v *ParameterMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v ParameterMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if qj, ok := target.(QueryJSONer); ok { 43 | return qj.QueryJSON(path) 44 | } 45 | 46 | if path == `` { 47 | return target, true 48 | } 49 | return nil, false 50 | } 51 | 52 | // UnmarshalJSON takes a JSON buffer and properly populates `v` 53 | func (v *ParameterMap) UnmarshalJSON(data []byte) error { 54 | var proxy map[string]*parameter 55 | if err := json.Unmarshal(data, &proxy); err != nil { 56 | return errors.Wrap(err, `failed to unmarshal`) 57 | } 58 | tmp := make(map[string]Parameter) 59 | for name, value := range proxy { 60 | tmp[name] = value 61 | } 62 | *v = tmp 63 | return nil 64 | } 65 | -------------------------------------------------------------------------------- /v2/parameter_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ParameterVisitor is an interface for objects that knows 16 | // how to process Parameter elements while traversing the OpenAPI structure 17 | type ParameterVisitor interface { 18 | VisitParameter(context.Context, Parameter) error 19 | } 20 | 21 | func visitParameter(ctx context.Context, elem Parameter) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(parameterVisitorCtxKey{}).(ParameterVisitor); ok { 34 | if err := v.VisitParameter(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit Parameter element`) 39 | } 40 | } 41 | 42 | if child := elem.Schema(); child != nil { 43 | if err := visitSchema(ctx, child); err != nil { 44 | return errors.Wrap(err, `failed to visit Schema element for Parameter`) 45 | } 46 | } 47 | 48 | if child := elem.Items(); child != nil { 49 | if err := visitItems(ctx, child); err != nil { 50 | return errors.Wrap(err, `failed to visit Items element for Parameter`) 51 | } 52 | } 53 | return nil 54 | } 55 | -------------------------------------------------------------------------------- /v2/path_item.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import "net/http" 4 | 5 | func (v *pathItem) setName(s string) { 6 | v.name = s 7 | } 8 | 9 | func (v *pathItem) acceptOperation(method string, oper Operation) { 10 | cloned := oper.Clone() 11 | cloned.setVerb(method) 12 | cloned.setPathItem(v) 13 | switch method { 14 | case http.MethodGet: 15 | v.get = cloned 16 | case http.MethodPut: 17 | v.put = cloned 18 | case http.MethodPost: 19 | v.post = cloned 20 | case http.MethodDelete: 21 | v.delete = cloned 22 | case http.MethodOptions: 23 | v.options = cloned 24 | case http.MethodHead: 25 | v.head = cloned 26 | case http.MethodPatch: 27 | v.patch = cloned 28 | } 29 | } 30 | 31 | // Operations returns an iterator that you can use to iterate through 32 | // all non-nil operations 33 | func (v *pathItem) Operations() *OperationListIterator { 34 | var items []interface{} 35 | for _, oper := range []Operation{v.get, v.put, v.post, v.delete, v.options, v.head, v.patch} { 36 | if oper != nil { 37 | items = append(items, oper) 38 | } 39 | } 40 | 41 | var iter OperationListIterator 42 | iter.items = items 43 | return &iter 44 | } 45 | -------------------------------------------------------------------------------- /v2/path_item_builder.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import "net/http" 4 | 5 | func (v *pathItem) setPath(s string) { 6 | v.path = s 7 | v.setVerb(http.MethodGet, v.get) 8 | v.setVerb(http.MethodPut, v.put) 9 | v.setVerb(http.MethodPost, v.post) 10 | v.setVerb(http.MethodDelete, v.delete) 11 | v.setVerb(http.MethodOptions, v.options) 12 | v.setVerb(http.MethodHead, v.head) 13 | v.setVerb(http.MethodPatch, v.patch) 14 | } 15 | 16 | func (v *pathItem) setVerb(verb string, oper Operation) { 17 | if oper == nil { 18 | return 19 | } 20 | oper.setVerb(verb) 21 | oper.setPathItem(v) 22 | } 23 | 24 | func (b *PathItemBuilder) Get(oper Operation) *PathItemBuilder { 25 | b.target.acceptOperation(http.MethodGet, oper) 26 | return b 27 | } 28 | 29 | func (b *PathItemBuilder) Put(v Operation) *PathItemBuilder { 30 | b.target.acceptOperation(http.MethodPut, v) 31 | return b 32 | } 33 | 34 | func (b *PathItemBuilder) Post(v Operation) *PathItemBuilder { 35 | b.target.acceptOperation(http.MethodPost, v) 36 | return b 37 | } 38 | 39 | func (b *PathItemBuilder) Delete(v Operation) *PathItemBuilder { 40 | b.target.acceptOperation(http.MethodDelete, v) 41 | return b 42 | } 43 | 44 | func (b *PathItemBuilder) Options(v Operation) *PathItemBuilder { 45 | b.target.acceptOperation(http.MethodOptions, v) 46 | return b 47 | } 48 | 49 | func (b *PathItemBuilder) Head(v Operation) *PathItemBuilder { 50 | b.target.acceptOperation(http.MethodHead, v) 51 | return b 52 | } 53 | 54 | func (b *PathItemBuilder) Patch(v Operation) *PathItemBuilder { 55 | b.target.acceptOperation(http.MethodPatch, v) 56 | return b 57 | } 58 | -------------------------------------------------------------------------------- /v2/path_item_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from PathItemMap 18 | func (v *PathItemMap) Clear() error { 19 | *v = make(PathItemMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in PathItemMap 24 | func (v *PathItemMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v PathItemMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if qj, ok := target.(QueryJSONer); ok { 43 | return qj.QueryJSON(path) 44 | } 45 | 46 | if path == `` { 47 | return target, true 48 | } 49 | return nil, false 50 | } 51 | 52 | // UnmarshalJSON takes a JSON buffer and properly populates `v` 53 | func (v *PathItemMap) UnmarshalJSON(data []byte) error { 54 | var proxy map[string]*pathItem 55 | if err := json.Unmarshal(data, &proxy); err != nil { 56 | return errors.Wrap(err, `failed to unmarshal`) 57 | } 58 | tmp := make(map[string]PathItem) 59 | for name, value := range proxy { 60 | value.setName(name) 61 | tmp[name] = value 62 | } 63 | *v = tmp 64 | return nil 65 | } 66 | -------------------------------------------------------------------------------- /v2/path_item_mutator.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import "net/http" 4 | 5 | func (m *PathItemMutator) Get(v Operation) *PathItemMutator { 6 | m.proxy.acceptOperation(http.MethodGet, v) 7 | return m 8 | } 9 | 10 | func (m *PathItemMutator) Put(v Operation) *PathItemMutator { 11 | m.proxy.acceptOperation(http.MethodPut, v) 12 | return m 13 | } 14 | 15 | func (m *PathItemMutator) Post(v Operation) *PathItemMutator { 16 | m.proxy.acceptOperation(http.MethodPost, v) 17 | return m 18 | } 19 | 20 | func (m *PathItemMutator) Delete(v Operation) *PathItemMutator { 21 | m.proxy.acceptOperation(http.MethodDelete, v) 22 | return m 23 | } 24 | 25 | func (m *PathItemMutator) Options(v Operation) *PathItemMutator { 26 | m.proxy.acceptOperation(http.MethodOptions, v) 27 | return m 28 | } 29 | 30 | func (m *PathItemMutator) Head(v Operation) *PathItemMutator { 31 | m.proxy.acceptOperation(http.MethodHead, v) 32 | return m 33 | } 34 | 35 | func (m *PathItemMutator) Patch(v Operation) *PathItemMutator { 36 | m.proxy.acceptOperation(http.MethodPatch, v) 37 | return m 38 | } 39 | 40 | func (m *PathItemMutator) Trace(v Operation) *PathItemMutator { 41 | m.proxy.acceptOperation(http.MethodTrace, v) 42 | return m 43 | } 44 | -------------------------------------------------------------------------------- /v2/paths.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/pkg/errors" 7 | ) 8 | 9 | func (v *paths) addPathItem(path string, item PathItem) { 10 | if v.paths == nil { 11 | v.paths = make(map[string]PathItem) 12 | } 13 | 14 | v.paths[path] = item.Clone() 15 | v.paths[path].setPath(path) 16 | } 17 | 18 | type pathItemKeyVisitorKey struct{} 19 | 20 | func visitPaths(ctx context.Context, v Paths) error { 21 | select { 22 | case <-ctx.Done(): 23 | return ctx.Err() 24 | default: 25 | } 26 | 27 | if visitor, ok := ctx.Value(pathsVisitorCtxKey{}).(PathsVisitor); ok { 28 | if err := visitor.VisitPaths(ctx, v); err != nil { 29 | if err == ErrVisitAbort { 30 | return nil 31 | } 32 | return errors.Wrap(err, `failed to visit Paths element`) 33 | } 34 | } 35 | 36 | for iter := v.Paths(); iter.Next(); { 37 | path, item := iter.Item() 38 | if err := visitPathItem(context.WithValue(ctx, pathItemKeyVisitorKey{}, path), item); err != nil { 39 | return errors.Wrapf(err, `failed to visit path %v`, path) 40 | } 41 | } 42 | return nil 43 | } 44 | -------------------------------------------------------------------------------- /v2/paths_builder.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // Path sets the path item for path `path` to `item` 4 | func (b *PathsBuilder) Path(path string, item PathItem) *PathsBuilder { 5 | b.target.addPathItem(path, item) 6 | return b 7 | } 8 | -------------------------------------------------------------------------------- /v2/paths_json.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "sort" 7 | "strconv" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | func (p *paths) MarshalJSON() ([]byte, error) { 13 | if p == nil { 14 | return []byte("null"), nil 15 | } 16 | 17 | var keys []string 18 | for key := range p.paths { 19 | keys = append(keys, key) 20 | } 21 | 22 | sort.Strings(keys) 23 | 24 | var buf bytes.Buffer 25 | buf.WriteByte('{') 26 | for i, key := range keys { 27 | buf.WriteString(strconv.Quote(key)) 28 | buf.WriteByte(':') 29 | if err := json.NewEncoder(&buf).Encode(p.paths[key]); err != nil { 30 | return nil, errors.Wrapf(err, `failed to encode paths.%s`, key) 31 | } 32 | 33 | if i < len(keys)-1 { 34 | buf.WriteByte(',') 35 | } 36 | } 37 | buf.WriteByte('}') 38 | return buf.Bytes(), nil 39 | } 40 | 41 | func (p *paths) UnmarshalJSON(data []byte) error { 42 | var m map[string]json.RawMessage 43 | if err := json.Unmarshal(data, &m); err != nil { 44 | return errors.Wrap(err, `failed to unmarshal JSON`) 45 | } 46 | 47 | tmp := &paths{ 48 | paths: make(map[string]PathItem), 49 | } 50 | mutator := MutatePaths(tmp) 51 | 52 | for path, data := range m { 53 | var pi pathItem 54 | if err := json.Unmarshal(data, &pi); err != nil { 55 | return errors.Wrap(err, `failed to unmarshal JSON`) 56 | } 57 | mutator.Path(path, &pi) 58 | } 59 | if err := mutator.Apply(); err != nil { 60 | return errors.Wrap(err, `failed to mutate paths`) 61 | } 62 | *p = *tmp 63 | return nil 64 | } 65 | -------------------------------------------------------------------------------- /v2/paths_json_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated by gentypes.go 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | "fmt" 9 | "github.com/pkg/errors" 10 | "log" 11 | "strconv" 12 | "strings" 13 | ) 14 | 15 | var _ = json.Unmarshal 16 | var _ = fmt.Fprintf 17 | var _ = log.Printf 18 | var _ = strconv.ParseInt 19 | var _ = errors.Cause 20 | 21 | func (v *paths) QueryJSON(path string) (ret interface{}, ok bool) { 22 | path = strings.TrimLeftFunc(path, func(r rune) bool { return r == '#' || r == '/' }) 23 | if path == "" { 24 | return v, true 25 | } 26 | return nil, false 27 | } 28 | 29 | // PathsFromJSON constructs a Paths from JSON buffer. `dst` must 30 | // be a pointer to `Paths` 31 | func PathsFromJSON(buf []byte, dst interface{}) error { 32 | v, ok := dst.(*Paths) 33 | if !ok { 34 | return errors.Errorf(`dst needs to be a pointer to Paths, but got %T`, dst) 35 | } 36 | var tmp paths 37 | if err := json.Unmarshal(buf, &tmp); err != nil { 38 | return errors.Wrap(err, `failed to unmarshal Paths`) 39 | } 40 | *v = &tmp 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /v2/paths_mutator.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | 4 | // Path sets the path item for path `path` to `item` 5 | func (m *PathsMutator) Path(path string, item PathItem) *PathsMutator { 6 | m.proxy.addPathItem(path, item) 7 | return m 8 | } 9 | -------------------------------------------------------------------------------- /v2/paths_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated by gentypes.go 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "sync" 8 | ) 9 | 10 | // PathsMutator is used to build an instance of Paths. The user must 11 | // call `Apply()` after providing all the necessary information to 12 | // the new instance of Paths with new values 13 | type PathsMutator struct { 14 | lock sync.Locker 15 | proxy *paths 16 | target *paths 17 | } 18 | 19 | // Apply finalizes the matuation process for Paths and returns the result 20 | func (m *PathsMutator) Apply() error { 21 | m.lock.Lock() 22 | defer m.lock.Unlock() 23 | *m.target = *m.proxy 24 | return nil 25 | } 26 | 27 | // MutatePaths creates a new mutator object for Paths 28 | // Operations on the mutator are safe to be used concurrently, except for 29 | // when calling `Apply()`, where the user is responsible for restricting access 30 | // to the target object to be mutated 31 | func MutatePaths(v Paths, options ...Option) *PathsMutator { 32 | var lock sync.Locker = &sync.Mutex{} 33 | for _, option := range options { 34 | switch option.Name() { 35 | case optkeyLocker: 36 | lock = option.Value().(sync.Locker) 37 | } 38 | } 39 | if lock == nil { 40 | lock = nilLock{} 41 | } 42 | return &PathsMutator{ 43 | lock: lock, 44 | target: v.(*paths), 45 | proxy: v.Clone().(*paths), 46 | } 47 | } 48 | 49 | // Extension sets an arbitrary extension field in Paths 50 | func (m *PathsMutator) Extension(name string, value interface{}) *PathsMutator { 51 | if m.proxy.extensions == nil { 52 | m.proxy.extensions = Extensions{} 53 | } 54 | m.proxy.extensions[name] = value 55 | return m 56 | } 57 | -------------------------------------------------------------------------------- /v2/paths_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // PathsVisitor is an interface for objects that knows 16 | // how to process Paths elements while traversing the OpenAPI structure 17 | type PathsVisitor interface { 18 | VisitPaths(context.Context, Paths) error 19 | } 20 | -------------------------------------------------------------------------------- /v2/primitive_types.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (p PrimitiveType) IsValid() bool { 4 | switch p { 5 | case Integer, Number, String, Boolean, Object, Array, File, Null: 6 | return true 7 | } 8 | return false 9 | } 10 | -------------------------------------------------------------------------------- /v2/resolver.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import ( 4 | "errors" 5 | "sync" 6 | ) 7 | 8 | type resolver struct { 9 | mu sync.RWMutex 10 | cache map[string]interface{} 11 | ctx Swagger 12 | } 13 | 14 | func NewResolver(ctx Swagger) Resolver { 15 | return &resolver{ 16 | cache: make(map[string]interface{}), 17 | ctx: ctx, 18 | } 19 | } 20 | 21 | func (r *resolver) Resolve(path string) (interface{}, error) { 22 | r.mu.RLock() 23 | if v, ok := r.cache[path]; ok { 24 | defer r.mu.RUnlock() 25 | return v, nil 26 | } 27 | r.mu.RUnlock() 28 | 29 | v, ok := r.ctx.QueryJSON(path) 30 | if !ok { 31 | return nil, errors.New(`could not resolve reference`) 32 | } 33 | 34 | r.mu.Lock() 35 | r.cache[path] = v 36 | r.mu.Unlock() 37 | return v, nil 38 | } 39 | -------------------------------------------------------------------------------- /v2/response.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *response) setName(s string) { 4 | v.name = s 5 | } 6 | 7 | func (v *response) setStatusCode(s string) { 8 | v.statusCode = s 9 | } 10 | -------------------------------------------------------------------------------- /v2/response_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from ResponseMap 18 | func (v *ResponseMap) Clear() error { 19 | *v = make(ResponseMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in ResponseMap 24 | func (v *ResponseMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v ResponseMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if qj, ok := target.(QueryJSONer); ok { 43 | return qj.QueryJSON(path) 44 | } 45 | 46 | if path == `` { 47 | return target, true 48 | } 49 | return nil, false 50 | } 51 | 52 | // UnmarshalJSON takes a JSON buffer and properly populates `v` 53 | func (v *ResponseMap) UnmarshalJSON(data []byte) error { 54 | var proxy map[string]*response 55 | if err := json.Unmarshal(data, &proxy); err != nil { 56 | return errors.Wrap(err, `failed to unmarshal`) 57 | } 58 | tmp := make(map[string]Response) 59 | for name, value := range proxy { 60 | value.setName(name) 61 | tmp[name] = value 62 | } 63 | *v = tmp 64 | return nil 65 | } 66 | -------------------------------------------------------------------------------- /v2/response_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ResponseVisitor is an interface for objects that knows 16 | // how to process Response elements while traversing the OpenAPI structure 17 | type ResponseVisitor interface { 18 | VisitResponse(context.Context, Response) error 19 | } 20 | 21 | func visitResponse(ctx context.Context, elem Response) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(responseVisitorCtxKey{}).(ResponseVisitor); ok { 34 | if err := v.VisitResponse(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit Response element`) 39 | } 40 | } 41 | 42 | if child := elem.Schema(); child != nil { 43 | if err := visitSchema(ctx, child); err != nil { 44 | return errors.Wrap(err, `failed to visit Schema element for Response`) 45 | } 46 | } 47 | 48 | for iter := elem.Headers(); iter.Next(); { 49 | key, value := iter.Item() 50 | if err := visitHeader(context.WithValue(ctx, headerMapKeyVisitorCtxKey{}, key), value); err != nil { 51 | return errors.Wrap(err, `failed to visit Headers element for Response`) 52 | } 53 | } 54 | return nil 55 | } 56 | -------------------------------------------------------------------------------- /v2/responses_builder.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // Response sets the response for status code `code` to `v` 4 | func (b *ResponsesBuilder) Response(code string, v Response) *ResponsesBuilder { 5 | b.target.setResponse(code, v) 6 | return b 7 | } 8 | -------------------------------------------------------------------------------- /v2/responses_json_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated by gentypes.go 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | "fmt" 9 | "github.com/pkg/errors" 10 | "log" 11 | "strconv" 12 | "strings" 13 | ) 14 | 15 | var _ = json.Unmarshal 16 | var _ = fmt.Fprintf 17 | var _ = log.Printf 18 | var _ = strconv.ParseInt 19 | var _ = errors.Cause 20 | 21 | func (v *responses) QueryJSON(path string) (ret interface{}, ok bool) { 22 | path = strings.TrimLeftFunc(path, func(r rune) bool { return r == '#' || r == '/' }) 23 | if path == "" { 24 | return v, true 25 | } 26 | 27 | var frag string 28 | if i := strings.Index(path, "/"); i > -1 { 29 | frag = path[:i] 30 | path = path[i+1:] 31 | } else { 32 | frag = path 33 | path = "" 34 | } 35 | 36 | var target interface{} 37 | 38 | switch frag { 39 | case "default": 40 | target = v.defaultValue 41 | default: 42 | return nil, false 43 | } 44 | 45 | if qj, ok := target.(QueryJSONer); ok { 46 | return qj.QueryJSON(path) 47 | } 48 | if path == "" { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | // ResponsesFromJSON constructs a Responses from JSON buffer. `dst` must 55 | // be a pointer to `Responses` 56 | func ResponsesFromJSON(buf []byte, dst interface{}) error { 57 | v, ok := dst.(*Responses) 58 | if !ok { 59 | return errors.Errorf(`dst needs to be a pointer to Responses, but got %T`, dst) 60 | } 61 | var tmp responses 62 | if err := json.Unmarshal(buf, &tmp); err != nil { 63 | return errors.Wrap(err, `failed to unmarshal Responses`) 64 | } 65 | *v = &tmp 66 | return nil 67 | } 68 | -------------------------------------------------------------------------------- /v2/responses_mutator.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (m *ResponsesMutator) Response(key ResponseMapKey, value Response) *ResponsesMutator { 4 | m.proxy.setResponse(key, value) 5 | return m 6 | } 7 | 8 | -------------------------------------------------------------------------------- /v2/responses_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ResponsesVisitor is an interface for objects that knows 16 | // how to process Responses elements while traversing the OpenAPI structure 17 | type ResponsesVisitor interface { 18 | VisitResponses(context.Context, Responses) error 19 | } 20 | 21 | func visitResponses(ctx context.Context, elem Responses) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(responsesVisitorCtxKey{}).(ResponsesVisitor); ok { 34 | if err := v.VisitResponses(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit Responses element`) 39 | } 40 | } 41 | 42 | if child := elem.Default(); child != nil { 43 | if err := visitResponse(ctx, child); err != nil { 44 | return errors.Wrap(err, `failed to visit Default element for Responses`) 45 | } 46 | } 47 | 48 | for iter := elem.Responses(); iter.Next(); { 49 | key, value := iter.Item() 50 | if err := visitResponse(context.WithValue(ctx, responseMapKeyVisitorCtxKey{}, key), value); err != nil { 51 | return errors.Wrap(err, `failed to visit Responses element for Responses`) 52 | } 53 | } 54 | return nil 55 | } 56 | -------------------------------------------------------------------------------- /v2/schema_builder.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (b *SchemaBuilder) Property(name string, prop Schema) *SchemaBuilder { 4 | if b.target.properties == nil { 5 | b.target.properties = SchemaMap{} 6 | } 7 | b.target.properties[name] = prop 8 | return b 9 | } -------------------------------------------------------------------------------- /v2/schema_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all values from SchemaList 18 | func (v *SchemaList) Clear() error { 19 | *v = SchemaList(nil) 20 | return nil 21 | } 22 | 23 | // Validate checks for the values for correctness. If `recurse` 24 | // is specified, child elements are also validated 25 | func (v *SchemaList) Validate(recurse bool) error { 26 | for i, elem := range *v { 27 | if validator, ok := elem.(Validator); ok { 28 | if err := validator.Validate(recurse); err != nil { 29 | return errors.Wrapf(err, `failed to validate element %d`, i) 30 | } 31 | } 32 | } 33 | return nil 34 | } 35 | 36 | // UnmarshalJSON defines how SchemaList is deserialized from JSON 37 | func (v *SchemaList) UnmarshalJSON(data []byte) error { 38 | var proxy []*schema 39 | if err := json.Unmarshal(data, &proxy); err != nil { 40 | return errors.Wrap(err, `failed to unmarshal`) 41 | } 42 | 43 | if len(proxy) == 0 { 44 | *v = SchemaList(nil) 45 | return nil 46 | } 47 | 48 | tmp := make(SchemaList, len(proxy)) 49 | for i, value := range proxy { 50 | tmp[i] = value 51 | } 52 | *v = tmp 53 | return nil 54 | } 55 | -------------------------------------------------------------------------------- /v2/schema_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from SchemaMap 18 | func (v *SchemaMap) Clear() error { 19 | *v = make(SchemaMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in SchemaMap 24 | func (v *SchemaMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v SchemaMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if qj, ok := target.(QueryJSONer); ok { 43 | return qj.QueryJSON(path) 44 | } 45 | 46 | if path == `` { 47 | return target, true 48 | } 49 | return nil, false 50 | } 51 | 52 | // UnmarshalJSON takes a JSON buffer and properly populates `v` 53 | func (v *SchemaMap) UnmarshalJSON(data []byte) error { 54 | var proxy map[string]*schema 55 | if err := json.Unmarshal(data, &proxy); err != nil { 56 | return errors.Wrap(err, `failed to unmarshal`) 57 | } 58 | tmp := make(map[string]Schema) 59 | for name, value := range proxy { 60 | value.setName(name) 61 | tmp[name] = value 62 | } 63 | *v = tmp 64 | return nil 65 | } 66 | -------------------------------------------------------------------------------- /v2/scheme_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all values from SchemeList 18 | func (v *SchemeList) Clear() error { 19 | *v = SchemeList(nil) 20 | return nil 21 | } 22 | 23 | // Validate checks for the values for correctness. If `recurse` 24 | // is specified, child elements are also validated 25 | func (v *SchemeList) Validate(recurse bool) error { 26 | return nil 27 | } 28 | -------------------------------------------------------------------------------- /v2/scopes_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from ScopesMap 18 | func (v *ScopesMap) Clear() error { 19 | *v = make(ScopesMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in ScopesMap 24 | func (v *ScopesMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v ScopesMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if path == `` { 43 | return target, true 44 | } 45 | return nil, false 46 | } 47 | -------------------------------------------------------------------------------- /v2/security_requirement.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import ( 4 | "encoding/json" 5 | "strconv" 6 | 7 | "github.com/pkg/errors" 8 | ) 9 | 10 | func (v *securityRequirement) setName(s string) { 11 | v.name = s 12 | } 13 | 14 | func (v *securityRequirement) UnmarshalJSON(data []byte) error { 15 | var m map[string]interface{} 16 | if err := json.Unmarshal(data, &m); err != nil { 17 | return errors.Wrap(err, `failed to unmarshal JSON data`) 18 | } 19 | 20 | if ref, ok := m["$ref"]; ok { 21 | if sref, ok := ref.(string); ok { 22 | v.reference = sref 23 | } else { 24 | return errors.Errorf(`invalid value for $ref: %T`, ref) 25 | } 26 | return nil 27 | } 28 | 29 | scopes := make(map[string][]string) 30 | for name, value := range m { 31 | list, ok := value.([]interface{}) 32 | if !ok { 33 | return errors.Errorf(`expected hash of arrays in security requirements, got %T`, value) 34 | } 35 | 36 | slist := make([]string, len(list)) 37 | for i, scope := range list { 38 | sscope, ok := scope.(string) 39 | if !ok { 40 | return errors.Errorf(`expected list of scopes (strings) in security requirements, got %T`, scope) 41 | } 42 | slist[i] = sscope 43 | } 44 | 45 | scopes[name] = slist 46 | } 47 | 48 | v.scopes = scopes 49 | return nil 50 | } 51 | 52 | func (v *securityRequirement) MarshalJSON() ([]byte, error) { 53 | if ref := v.reference; ref != "" { 54 | return []byte(`{"$ref": ` + strconv.Quote(ref) + `}`), nil 55 | } 56 | 57 | return json.Marshal(v.scopes) 58 | } 59 | -------------------------------------------------------------------------------- /v2/security_requirement_json_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated by gentypes.go 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | "fmt" 9 | "github.com/pkg/errors" 10 | "log" 11 | "strconv" 12 | "strings" 13 | ) 14 | 15 | var _ = json.Unmarshal 16 | var _ = fmt.Fprintf 17 | var _ = log.Printf 18 | var _ = strconv.ParseInt 19 | var _ = errors.Cause 20 | 21 | func (v *securityRequirement) QueryJSON(path string) (ret interface{}, ok bool) { 22 | path = strings.TrimLeftFunc(path, func(r rune) bool { return r == '#' || r == '/' }) 23 | if path == "" { 24 | return v, true 25 | } 26 | return nil, false 27 | } 28 | 29 | // SecurityRequirementFromJSON constructs a SecurityRequirement from JSON buffer. `dst` must 30 | // be a pointer to `SecurityRequirement` 31 | func SecurityRequirementFromJSON(buf []byte, dst interface{}) error { 32 | v, ok := dst.(*SecurityRequirement) 33 | if !ok { 34 | return errors.Errorf(`dst needs to be a pointer to SecurityRequirement, but got %T`, dst) 35 | } 36 | var tmp securityRequirement 37 | if err := json.Unmarshal(buf, &tmp); err != nil { 38 | return errors.Wrap(err, `failed to unmarshal SecurityRequirement`) 39 | } 40 | *v = &tmp 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /v2/security_requirement_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all values from SecurityRequirementList 18 | func (v *SecurityRequirementList) Clear() error { 19 | *v = SecurityRequirementList(nil) 20 | return nil 21 | } 22 | 23 | // Validate checks for the values for correctness. If `recurse` 24 | // is specified, child elements are also validated 25 | func (v *SecurityRequirementList) Validate(recurse bool) error { 26 | for i, elem := range *v { 27 | if validator, ok := elem.(Validator); ok { 28 | if err := validator.Validate(recurse); err != nil { 29 | return errors.Wrapf(err, `failed to validate element %d`, i) 30 | } 31 | } 32 | } 33 | return nil 34 | } 35 | 36 | // UnmarshalJSON defines how SecurityRequirementList is deserialized from JSON 37 | func (v *SecurityRequirementList) UnmarshalJSON(data []byte) error { 38 | var proxy []*securityRequirement 39 | if err := json.Unmarshal(data, &proxy); err != nil { 40 | return errors.Wrap(err, `failed to unmarshal`) 41 | } 42 | 43 | if len(proxy) == 0 { 44 | *v = SecurityRequirementList(nil) 45 | return nil 46 | } 47 | 48 | tmp := make(SecurityRequirementList, len(proxy)) 49 | for i, value := range proxy { 50 | tmp[i] = value 51 | } 52 | *v = tmp 53 | return nil 54 | } 55 | -------------------------------------------------------------------------------- /v2/security_requirement_test.go: -------------------------------------------------------------------------------- 1 | package openapi_test 2 | 3 | import ( 4 | "testing" 5 | 6 | openapi "github.com/lestrrat-go/openapi/v2" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func TestSecurityRequirementJSON(t *testing.T) { 11 | const src = `{ 12 | "foo": ["AAA", "BBB", "CCC"], 13 | "bar": ["DDD"], 14 | "baz": [] 15 | }` 16 | 17 | var sr openapi.SecurityRequirement 18 | if !assert.NoError(t, openapi.SecurityRequirementFromJSON([]byte(src), &sr), `unmarshaling JSON should work`) { 19 | return 20 | } 21 | 22 | t.Logf("%#v", sr) 23 | } 24 | -------------------------------------------------------------------------------- /v2/security_requirement_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // SecurityRequirementVisitor is an interface for objects that knows 16 | // how to process SecurityRequirement elements while traversing the OpenAPI structure 17 | type SecurityRequirementVisitor interface { 18 | VisitSecurityRequirement(context.Context, SecurityRequirement) error 19 | } 20 | 21 | func visitSecurityRequirement(ctx context.Context, elem SecurityRequirement) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(securityRequirementVisitorCtxKey{}).(SecurityRequirementVisitor); ok { 34 | if err := v.VisitSecurityRequirement(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit SecurityRequirement element`) 39 | } 40 | } 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /v2/security_scheme_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from SecuritySchemeMap 18 | func (v *SecuritySchemeMap) Clear() error { 19 | *v = make(SecuritySchemeMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in SecuritySchemeMap 24 | func (v *SecuritySchemeMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v SecuritySchemeMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if qj, ok := target.(QueryJSONer); ok { 43 | return qj.QueryJSON(path) 44 | } 45 | 46 | if path == `` { 47 | return target, true 48 | } 49 | return nil, false 50 | } 51 | 52 | // UnmarshalJSON takes a JSON buffer and properly populates `v` 53 | func (v *SecuritySchemeMap) UnmarshalJSON(data []byte) error { 54 | var proxy map[string]*securityScheme 55 | if err := json.Unmarshal(data, &proxy); err != nil { 56 | return errors.Wrap(err, `failed to unmarshal`) 57 | } 58 | tmp := make(map[string]SecurityScheme) 59 | for name, value := range proxy { 60 | tmp[name] = value 61 | } 62 | *v = tmp 63 | return nil 64 | } 65 | -------------------------------------------------------------------------------- /v2/security_scheme_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // SecuritySchemeVisitor is an interface for objects that knows 16 | // how to process SecurityScheme elements while traversing the OpenAPI structure 17 | type SecuritySchemeVisitor interface { 18 | VisitSecurityScheme(context.Context, SecurityScheme) error 19 | } 20 | 21 | func visitSecurityScheme(ctx context.Context, elem SecurityScheme) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(securitySchemeVisitorCtxKey{}).(SecuritySchemeVisitor); ok { 34 | if err := v.VisitSecurityScheme(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit SecurityScheme element`) 39 | } 40 | } 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /v2/string_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all values from StringList 18 | func (v *StringList) Clear() error { 19 | *v = StringList(nil) 20 | return nil 21 | } 22 | 23 | // Validate checks for the values for correctness. If `recurse` 24 | // is specified, child elements are also validated 25 | func (v *StringList) Validate(recurse bool) error { 26 | return nil 27 | } 28 | -------------------------------------------------------------------------------- /v2/string_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all elements from StringMap 18 | func (v *StringMap) Clear() error { 19 | *v = make(StringMap) 20 | return nil 21 | } 22 | 23 | // Validate checks the correctness of values in StringMap 24 | func (v *StringMap) Validate(recurse bool) error { 25 | return Visit(context.Background(), newValidator(recurse), v) 26 | } 27 | 28 | // QueryJSON is used to query an element within the document 29 | // Using jsonref 30 | func (v StringMap) QueryJSON(path string) (ret interface{}, ok bool) { 31 | if path == `` { 32 | return v, true 33 | } 34 | 35 | var frag string 36 | frag, path = extractFragFromPath(path) 37 | target, ok := v[frag] 38 | if !ok { 39 | return nil, false 40 | } 41 | 42 | if path == `` { 43 | return target, true 44 | } 45 | return nil, false 46 | } 47 | -------------------------------------------------------------------------------- /v2/swagger_builder_test.go: -------------------------------------------------------------------------------- 1 | package openapi_test 2 | 3 | import ( 4 | "testing" 5 | 6 | openapi "github.com/lestrrat-go/openapi/v2" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func TestSwaggerValidate(t *testing.T) { 11 | t.Run("invalid info", func(t *testing.T) { 12 | _, err := openapi.NewSwagger(nil, petsPaths).Build() 13 | if !assert.Error(t, err, `info = nil should result in error`) { 14 | return 15 | } 16 | }) 17 | t.Run("valid host", func(t *testing.T) { 18 | for _, h := range []string{"example.com", "example.com:8080"} { 19 | _, err := openapi.NewSwagger(petsInfo, petsPaths). 20 | Host(h). 21 | Build() 22 | if !assert.NoError(t, err, `valid host %s should NOT result in error`, h) { 23 | return 24 | } 25 | } 26 | }) 27 | t.Run("invalid host", func(t *testing.T) { 28 | for _, h := range []string{"https://example.com", "example.com/foo/bar"} { 29 | _, err := openapi.NewSwagger(petsInfo, petsPaths). 30 | Host(h). 31 | Build() 32 | if !assert.Error(t, err, `invalid host %s should result in error`, h) { 33 | return 34 | } 35 | } 36 | }) 37 | t.Run("invalid basePath", func(t *testing.T) { 38 | _, err := openapi.NewSwagger(petsInfo, petsPaths). 39 | BasePath("foo"). 40 | Build() 41 | if !assert.Error(t, err, `invalid basePath %s should result in error`, "foo") { 42 | return 43 | } 44 | }) 45 | t.Run("invalid paths", func(t *testing.T) { 46 | _, err := openapi.NewSwagger(petsInfo, nil). 47 | Build() 48 | if !assert.Error(t, err, `paths = nil should result in error`) { 49 | return 50 | } 51 | }) 52 | } 53 | -------------------------------------------------------------------------------- /v2/tag_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "sort" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = sort.Strings 15 | var _ = errors.Cause 16 | 17 | func (v *tag) IsValid() bool { 18 | return v != nil 19 | } 20 | 21 | func (v *tag) Name() string { 22 | return v.name 23 | } 24 | 25 | func (v *tag) Description() string { 26 | return v.description 27 | } 28 | 29 | func (v *tag) ExternalDocs() ExternalDocumentation { 30 | return v.externalDocs 31 | } 32 | 33 | // Reference returns the value of `$ref` field 34 | func (v *tag) Reference() string { 35 | return v.reference 36 | } 37 | 38 | func (v *tag) IsUnresolved() bool { 39 | return v.reference != "" && !v.resolved 40 | } 41 | 42 | // Extension returns the value of an arbitrary extension 43 | func (v *tag) Extension(key string) (interface{}, bool) { 44 | e, ok := v.extensions[key] 45 | return e, ok 46 | } 47 | 48 | // Extensions return an iterator to iterate over all extensions 49 | func (v *tag) Extensions() *ExtensionsIterator { 50 | var items []interface{} 51 | for key, item := range v.extensions { 52 | items = append(items, &mapIteratorItem{key: key, item: item}) 53 | } 54 | var iter ExtensionsIterator 55 | iter.list.size = len(items) 56 | iter.list.items = items 57 | return &iter 58 | } 59 | 60 | func (v *tag) Validate(recurse bool) error { 61 | return newValidator(recurse).Validate(context.Background(), v) 62 | } 63 | -------------------------------------------------------------------------------- /v2/tag_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "encoding/json" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = json.Unmarshal 15 | var _ = errors.Cause 16 | 17 | // Clear removes all values from TagList 18 | func (v *TagList) Clear() error { 19 | *v = TagList(nil) 20 | return nil 21 | } 22 | 23 | // Validate checks for the values for correctness. If `recurse` 24 | // is specified, child elements are also validated 25 | func (v *TagList) Validate(recurse bool) error { 26 | for i, elem := range *v { 27 | if validator, ok := elem.(Validator); ok { 28 | if err := validator.Validate(recurse); err != nil { 29 | return errors.Wrapf(err, `failed to validate element %d`, i) 30 | } 31 | } 32 | } 33 | return nil 34 | } 35 | 36 | // UnmarshalJSON defines how TagList is deserialized from JSON 37 | func (v *TagList) UnmarshalJSON(data []byte) error { 38 | var proxy []*tag 39 | if err := json.Unmarshal(data, &proxy); err != nil { 40 | return errors.Wrap(err, `failed to unmarshal`) 41 | } 42 | 43 | if len(proxy) == 0 { 44 | *v = TagList(nil) 45 | return nil 46 | } 47 | 48 | tmp := make(TagList, len(proxy)) 49 | for i, value := range proxy { 50 | tmp[i] = value 51 | } 52 | *v = tmp 53 | return nil 54 | } 55 | -------------------------------------------------------------------------------- /v2/tag_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // TagVisitor is an interface for objects that knows 16 | // how to process Tag elements while traversing the OpenAPI structure 17 | type TagVisitor interface { 18 | VisitTag(context.Context, Tag) error 19 | } 20 | 21 | func visitTag(ctx context.Context, elem Tag) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(tagVisitorCtxKey{}).(TagVisitor); ok { 34 | if err := v.VisitTag(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit Tag element`) 39 | } 40 | } 41 | 42 | if child := elem.ExternalDocs(); child != nil { 43 | if err := visitExternalDocumentation(ctx, child); err != nil { 44 | return errors.Wrap(err, `failed to visit ExternalDocs element for Tag`) 45 | } 46 | } 47 | return nil 48 | } 49 | -------------------------------------------------------------------------------- /v2/xml_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | "sort" 9 | 10 | "github.com/pkg/errors" 11 | ) 12 | 13 | var _ = context.Background 14 | var _ = sort.Strings 15 | var _ = errors.Cause 16 | 17 | func (v *xml) IsValid() bool { 18 | return v != nil 19 | } 20 | 21 | func (v *xml) Name() string { 22 | return v.name 23 | } 24 | 25 | func (v *xml) Namespace() string { 26 | return v.namespace 27 | } 28 | 29 | func (v *xml) Prefix() string { 30 | return v.prefix 31 | } 32 | 33 | func (v *xml) Attribute() bool { 34 | return v.attribute 35 | } 36 | 37 | func (v *xml) Wrapped() bool { 38 | return v.wrapped 39 | } 40 | 41 | // Reference returns the value of `$ref` field 42 | func (v *xml) Reference() string { 43 | return v.reference 44 | } 45 | 46 | func (v *xml) IsUnresolved() bool { 47 | return v.reference != "" && !v.resolved 48 | } 49 | 50 | // Extension returns the value of an arbitrary extension 51 | func (v *xml) Extension(key string) (interface{}, bool) { 52 | e, ok := v.extensions[key] 53 | return e, ok 54 | } 55 | 56 | // Extensions return an iterator to iterate over all extensions 57 | func (v *xml) Extensions() *ExtensionsIterator { 58 | var items []interface{} 59 | for key, item := range v.extensions { 60 | items = append(items, &mapIteratorItem{key: key, item: item}) 61 | } 62 | var iter ExtensionsIterator 63 | iter.list.size = len(items) 64 | iter.list.items = items 65 | return &iter 66 | } 67 | 68 | func (v *xml) Validate(recurse bool) error { 69 | return newValidator(recurse).Validate(context.Background(), v) 70 | } 71 | -------------------------------------------------------------------------------- /v2/xml_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // XMLVisitor is an interface for objects that knows 16 | // how to process XML elements while traversing the OpenAPI structure 17 | type XMLVisitor interface { 18 | VisitXML(context.Context, XML) error 19 | } 20 | 21 | func visitXML(ctx context.Context, elem XML) error { 22 | if checker, ok := elem.(interface{ IsValid() bool }); ok { 23 | if !checker.IsValid() { 24 | return nil 25 | } 26 | } 27 | select { 28 | case <-ctx.Done(): 29 | return ctx.Err() 30 | default: 31 | } 32 | 33 | if v, ok := ctx.Value(xmlVisitorCtxKey{}).(XMLVisitor); ok { 34 | if err := v.VisitXML(ctx, elem); err != nil { 35 | if err == ErrVisitAbort { 36 | return nil 37 | } 38 | return errors.Wrap(err, `failed to visit XML element`) 39 | } 40 | } 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /v3/callback.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (c *callback) setName(s string) { 4 | c.name = s 5 | } -------------------------------------------------------------------------------- /v3/callback_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *callback) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *callback) URLs() map[string]PathItem { 20 | return v.urls 21 | } 22 | 23 | func (v *callback) Reference() string { 24 | return v.reference 25 | } 26 | 27 | func (v *callback) IsUnresolved() bool { 28 | return v.reference != "" && !v.resolved 29 | } 30 | 31 | func (v *callback) Validate(recurse bool) error { 32 | return Visit(context.Background(), newValidator(recurse), v) 33 | } 34 | -------------------------------------------------------------------------------- /v3/callback_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *CallbackMap) Clear() error { 16 | *v = make(CallbackMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in CallbackMap 21 | func (v *CallbackMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v CallbackMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *CallbackMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*callback 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal CallbackMap`) 58 | } 59 | tmp := make(map[string]Callback) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/callback_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // CallbackMutator is used to build an instance of Callback. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of Callback with new values 13 | type CallbackMutator struct { 14 | proxy *callback 15 | target *callback 16 | } 17 | 18 | // Do finalizes the matuation process for Callback and returns the result 19 | func (b *CallbackMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateCallback creates a new mutator object for Callback 25 | func MutateCallback(v Callback) *CallbackMutator { 26 | return &CallbackMutator{ 27 | target: v.(*callback), 28 | proxy: v.Clone().(*callback), 29 | } 30 | } 31 | 32 | // Name sets the Name field for object Callback. 33 | func (b *CallbackMutator) Name(v string) *CallbackMutator { 34 | b.proxy.name = v 35 | return b 36 | } 37 | 38 | // URLs sets the URLs field for object Callback. 39 | func (b *CallbackMutator) URLs(v map[string]PathItem) *CallbackMutator { 40 | b.proxy.urls = v 41 | return b 42 | } 43 | -------------------------------------------------------------------------------- /v3/callback_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // CallbackVisitor is an interface for objects that knows 16 | // how to process Callback elements while traversing the OpenAPI structure 17 | type CallbackVisitor interface { 18 | VisitCallback(context.Context, Callback) error 19 | } 20 | 21 | func visitCallback(ctx context.Context, elem Callback) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(callbackVisitorCtxKey{}).(CallbackVisitor); ok { 29 | if err := v.VisitCallback(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Callback element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/components_builder.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // Schema sets the schema identified by `name` to `s` 4 | func (b *ComponentsBuilder) Schema(name string, s Schema) *ComponentsBuilder { 5 | if b.target.schemas == nil { 6 | b.target.schemas = make(map[string]Schema) 7 | } 8 | s = s.Clone() 9 | b.target.schemas[name] = s 10 | s.setName(name) 11 | return b 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v3/components_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ComponentsVisitor is an interface for objects that knows 16 | // how to process Components elements while traversing the OpenAPI structure 17 | type ComponentsVisitor interface { 18 | VisitComponents(context.Context, Components) error 19 | } 20 | 21 | func visitComponents(ctx context.Context, elem Components) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(componentsVisitorCtxKey{}).(ComponentsVisitor); ok { 29 | if err := v.VisitComponents(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Components element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/contact_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *contact) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *contact) URL() string { 20 | return v.url 21 | } 22 | 23 | func (v *contact) Email() string { 24 | return v.email 25 | } 26 | 27 | func (v *contact) Reference() string { 28 | return v.reference 29 | } 30 | 31 | func (v *contact) IsUnresolved() bool { 32 | return v.reference != "" && !v.resolved 33 | } 34 | 35 | func (v *contact) Validate(recurse bool) error { 36 | return Visit(context.Background(), newValidator(recurse), v) 37 | } 38 | -------------------------------------------------------------------------------- /v3/contact_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // ContactMutator is used to build an instance of Contact. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of Contact with new values 13 | type ContactMutator struct { 14 | proxy *contact 15 | target *contact 16 | } 17 | 18 | // Do finalizes the matuation process for Contact and returns the result 19 | func (b *ContactMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateContact creates a new mutator object for Contact 25 | func MutateContact(v Contact) *ContactMutator { 26 | return &ContactMutator{ 27 | target: v.(*contact), 28 | proxy: v.Clone().(*contact), 29 | } 30 | } 31 | 32 | // Name sets the Name field for object Contact. 33 | func (b *ContactMutator) Name(v string) *ContactMutator { 34 | b.proxy.name = v 35 | return b 36 | } 37 | 38 | // URL sets the URL field for object Contact. 39 | func (b *ContactMutator) URL(v string) *ContactMutator { 40 | b.proxy.url = v 41 | return b 42 | } 43 | 44 | // Email sets the Email field for object Contact. 45 | func (b *ContactMutator) Email(v string) *ContactMutator { 46 | b.proxy.email = v 47 | return b 48 | } 49 | -------------------------------------------------------------------------------- /v3/contact_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ContactVisitor is an interface for objects that knows 16 | // how to process Contact elements while traversing the OpenAPI structure 17 | type ContactVisitor interface { 18 | VisitContact(context.Context, Contact) error 19 | } 20 | 21 | func visitContact(ctx context.Context, elem Contact) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(contactVisitorCtxKey{}).(ContactVisitor); ok { 29 | if err := v.VisitContact(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Contact element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/discriminator_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *discriminator) PropertyName() string { 16 | return v.propertyName 17 | } 18 | 19 | func (v *discriminator) Mapping() *StringMapIterator { 20 | var items []interface{} 21 | for key, item := range v.mapping { 22 | items = append(items, &mapIteratorItem{key: key, item: item}) 23 | } 24 | var iter StringMapIterator 25 | iter.list.items = items 26 | return &iter 27 | } 28 | 29 | func (v *discriminator) Reference() string { 30 | return v.reference 31 | } 32 | 33 | func (v *discriminator) IsUnresolved() bool { 34 | return v.reference != "" && !v.resolved 35 | } 36 | 37 | func (v *discriminator) Validate(recurse bool) error { 38 | return Visit(context.Background(), newValidator(recurse), v) 39 | } 40 | -------------------------------------------------------------------------------- /v3/discriminator_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // DiscriminatorMutator is used to build an instance of Discriminator. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of Discriminator with new values 13 | type DiscriminatorMutator struct { 14 | proxy *discriminator 15 | target *discriminator 16 | } 17 | 18 | // Do finalizes the matuation process for Discriminator and returns the result 19 | func (b *DiscriminatorMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateDiscriminator creates a new mutator object for Discriminator 25 | func MutateDiscriminator(v Discriminator) *DiscriminatorMutator { 26 | return &DiscriminatorMutator{ 27 | target: v.(*discriminator), 28 | proxy: v.Clone().(*discriminator), 29 | } 30 | } 31 | 32 | // PropertyName sets the PropertyName field for object Discriminator. 33 | func (b *DiscriminatorMutator) PropertyName(v string) *DiscriminatorMutator { 34 | b.proxy.propertyName = v 35 | return b 36 | } 37 | 38 | func (b *DiscriminatorMutator) ClearMapping() *DiscriminatorMutator { 39 | b.proxy.mapping.Clear() 40 | return b 41 | } 42 | 43 | func (b *DiscriminatorMutator) Mapping(key StringMapKey, value string) *DiscriminatorMutator { 44 | if b.proxy.mapping == nil { 45 | b.proxy.mapping = StringMap{} 46 | } 47 | 48 | b.proxy.mapping[key] = value 49 | return b 50 | } 51 | -------------------------------------------------------------------------------- /v3/discriminator_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // DiscriminatorVisitor is an interface for objects that knows 16 | // how to process Discriminator elements while traversing the OpenAPI structure 17 | type DiscriminatorVisitor interface { 18 | VisitDiscriminator(context.Context, Discriminator) error 19 | } 20 | 21 | func visitDiscriminator(ctx context.Context, elem Discriminator) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(discriminatorVisitorCtxKey{}).(DiscriminatorVisitor); ok { 29 | if err := v.VisitDiscriminator(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Discriminator element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/encoding.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *encoding) setName(s string) { 4 | v.name = s 5 | } 6 | -------------------------------------------------------------------------------- /v3/encoding_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *encoding) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *encoding) ContentType() string { 20 | return v.contentType 21 | } 22 | 23 | func (v *encoding) Headers() *HeaderMapIterator { 24 | var items []interface{} 25 | for key, item := range v.headers { 26 | items = append(items, &mapIteratorItem{key: key, item: item}) 27 | } 28 | var iter HeaderMapIterator 29 | iter.list.items = items 30 | return &iter 31 | } 32 | 33 | func (v *encoding) Explode() bool { 34 | return v.explode 35 | } 36 | 37 | func (v *encoding) AllowReserved() bool { 38 | return v.allowReserved 39 | } 40 | 41 | func (v *encoding) Reference() string { 42 | return v.reference 43 | } 44 | 45 | func (v *encoding) IsUnresolved() bool { 46 | return v.reference != "" && !v.resolved 47 | } 48 | 49 | func (v *encoding) Validate(recurse bool) error { 50 | return Visit(context.Background(), newValidator(recurse), v) 51 | } 52 | -------------------------------------------------------------------------------- /v3/encoding_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *EncodingMap) Clear() error { 16 | *v = make(EncodingMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in EncodingMap 21 | func (v *EncodingMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v EncodingMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *EncodingMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*encoding 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal EncodingMap`) 58 | } 59 | tmp := make(map[string]Encoding) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/encoding_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // EncodingVisitor is an interface for objects that knows 16 | // how to process Encoding elements while traversing the OpenAPI structure 17 | type EncodingVisitor interface { 18 | VisitEncoding(context.Context, Encoding) error 19 | } 20 | 21 | func visitEncoding(ctx context.Context, elem Encoding) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(encodingVisitorCtxKey{}).(EncodingVisitor); ok { 29 | if err := v.VisitEncoding(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Encoding element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/entity.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | -------------------------------------------------------------------------------- /v3/example.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *example) setName(s string) { 4 | v.name = s 5 | } -------------------------------------------------------------------------------- /v3/example_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *example) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *example) Description() string { 20 | return v.description 21 | } 22 | 23 | func (v *example) Value() interface{} { 24 | return v.value 25 | } 26 | 27 | func (v *example) ExternalValue() string { 28 | return v.externalValue 29 | } 30 | 31 | func (v *example) Reference() string { 32 | return v.reference 33 | } 34 | 35 | func (v *example) IsUnresolved() bool { 36 | return v.reference != "" && !v.resolved 37 | } 38 | 39 | func (v *example) Validate(recurse bool) error { 40 | return Visit(context.Background(), newValidator(recurse), v) 41 | } 42 | -------------------------------------------------------------------------------- /v3/example_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *ExampleMap) Clear() error { 16 | *v = make(ExampleMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in ExampleMap 21 | func (v *ExampleMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v ExampleMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *ExampleMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*example 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal ExampleMap`) 58 | } 59 | tmp := make(map[string]Example) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/example_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // ExampleMutator is used to build an instance of Example. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of Example with new values 13 | type ExampleMutator struct { 14 | proxy *example 15 | target *example 16 | } 17 | 18 | // Do finalizes the matuation process for Example and returns the result 19 | func (b *ExampleMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateExample creates a new mutator object for Example 25 | func MutateExample(v Example) *ExampleMutator { 26 | return &ExampleMutator{ 27 | target: v.(*example), 28 | proxy: v.Clone().(*example), 29 | } 30 | } 31 | 32 | // Name sets the Name field for object Example. 33 | func (b *ExampleMutator) Name(v string) *ExampleMutator { 34 | b.proxy.name = v 35 | return b 36 | } 37 | 38 | // Description sets the Description field for object Example. 39 | func (b *ExampleMutator) Description(v string) *ExampleMutator { 40 | b.proxy.description = v 41 | return b 42 | } 43 | 44 | // Value sets the Value field for object Example. 45 | func (b *ExampleMutator) Value(v interface{}) *ExampleMutator { 46 | b.proxy.value = v 47 | return b 48 | } 49 | 50 | // ExternalValue sets the ExternalValue field for object Example. 51 | func (b *ExampleMutator) ExternalValue(v string) *ExampleMutator { 52 | b.proxy.externalValue = v 53 | return b 54 | } 55 | -------------------------------------------------------------------------------- /v3/example_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ExampleVisitor is an interface for objects that knows 16 | // how to process Example elements while traversing the OpenAPI structure 17 | type ExampleVisitor interface { 18 | VisitExample(context.Context, Example) error 19 | } 20 | 21 | func visitExample(ctx context.Context, elem Example) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(exampleVisitorCtxKey{}).(ExampleVisitor); ok { 29 | if err := v.VisitExample(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Example element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/external_documentation_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *externalDocumentation) Description() string { 16 | return v.description 17 | } 18 | 19 | func (v *externalDocumentation) URL() string { 20 | return v.url 21 | } 22 | 23 | func (v *externalDocumentation) Reference() string { 24 | return v.reference 25 | } 26 | 27 | func (v *externalDocumentation) IsUnresolved() bool { 28 | return v.reference != "" && !v.resolved 29 | } 30 | 31 | func (v *externalDocumentation) Validate(recurse bool) error { 32 | return Visit(context.Background(), newValidator(recurse), v) 33 | } 34 | -------------------------------------------------------------------------------- /v3/external_documentation_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // ExternalDocumentationMutator is used to build an instance of ExternalDocumentation. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of ExternalDocumentation with new values 13 | type ExternalDocumentationMutator struct { 14 | proxy *externalDocumentation 15 | target *externalDocumentation 16 | } 17 | 18 | // Do finalizes the matuation process for ExternalDocumentation and returns the result 19 | func (b *ExternalDocumentationMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateExternalDocumentation creates a new mutator object for ExternalDocumentation 25 | func MutateExternalDocumentation(v ExternalDocumentation) *ExternalDocumentationMutator { 26 | return &ExternalDocumentationMutator{ 27 | target: v.(*externalDocumentation), 28 | proxy: v.Clone().(*externalDocumentation), 29 | } 30 | } 31 | 32 | // Description sets the Description field for object ExternalDocumentation. 33 | func (b *ExternalDocumentationMutator) Description(v string) *ExternalDocumentationMutator { 34 | b.proxy.description = v 35 | return b 36 | } 37 | 38 | // URL sets the URL field for object ExternalDocumentation. 39 | func (b *ExternalDocumentationMutator) URL(v string) *ExternalDocumentationMutator { 40 | b.proxy.url = v 41 | return b 42 | } 43 | -------------------------------------------------------------------------------- /v3/external_documentation_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ExternalDocumentationVisitor is an interface for objects that knows 16 | // how to process ExternalDocumentation elements while traversing the OpenAPI structure 17 | type ExternalDocumentationVisitor interface { 18 | VisitExternalDocumentation(context.Context, ExternalDocumentation) error 19 | } 20 | 21 | func visitExternalDocumentation(ctx context.Context, elem ExternalDocumentation) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(externalDocumentationVisitorCtxKey{}).(ExternalDocumentationVisitor); ok { 29 | if err := v.VisitExternalDocumentation(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit ExternalDocumentation element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/header.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (h *header) setName(s string) { 4 | h.name = s 5 | } -------------------------------------------------------------------------------- /v3/header_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *HeaderMap) Clear() error { 16 | *v = make(HeaderMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in HeaderMap 21 | func (v *HeaderMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v HeaderMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *HeaderMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*header 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal HeaderMap`) 58 | } 59 | tmp := make(map[string]Header) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/header_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // HeaderVisitor is an interface for objects that knows 16 | // how to process Header elements while traversing the OpenAPI structure 17 | type HeaderVisitor interface { 18 | VisitHeader(context.Context, Header) error 19 | } 20 | 21 | func visitHeader(ctx context.Context, elem Header) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(headerVisitorCtxKey{}).(HeaderVisitor); ok { 29 | if err := v.VisitHeader(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Header element`) 34 | } 35 | } 36 | 37 | if child := elem.Schema(); child != nil { 38 | if err := visitSchema(ctx, child); err != nil { 39 | return errors.Wrap(err, `failed to visit Schema element for Header`) 40 | } 41 | } 42 | return nil 43 | } 44 | -------------------------------------------------------------------------------- /v3/info_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *info) Title() string { 16 | return v.title 17 | } 18 | 19 | func (v *info) Description() string { 20 | return v.description 21 | } 22 | 23 | func (v *info) TermsOfService() string { 24 | return v.termsOfService 25 | } 26 | 27 | func (v *info) Contact() Contact { 28 | return v.contact 29 | } 30 | 31 | func (v *info) License() License { 32 | return v.license 33 | } 34 | 35 | func (v *info) Version() string { 36 | return v.version 37 | } 38 | 39 | func (v *info) Reference() string { 40 | return v.reference 41 | } 42 | 43 | func (v *info) IsUnresolved() bool { 44 | return v.reference != "" && !v.resolved 45 | } 46 | 47 | func (v *info) Validate(recurse bool) error { 48 | return Visit(context.Background(), newValidator(recurse), v) 49 | } 50 | -------------------------------------------------------------------------------- /v3/info_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // InfoVisitor is an interface for objects that knows 16 | // how to process Info elements while traversing the OpenAPI structure 17 | type InfoVisitor interface { 18 | VisitInfo(context.Context, Info) error 19 | } 20 | 21 | func visitInfo(ctx context.Context, elem Info) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(infoVisitorCtxKey{}).(InfoVisitor); ok { 29 | if err := v.VisitInfo(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Info element`) 34 | } 35 | } 36 | 37 | if child := elem.Contact(); child != nil { 38 | if err := visitContact(ctx, child); err != nil { 39 | return errors.Wrap(err, `failed to visit Contact element for Info`) 40 | } 41 | } 42 | 43 | if child := elem.License(); child != nil { 44 | if err := visitLicense(ctx, child); err != nil { 45 | return errors.Wrap(err, `failed to visit License element for Info`) 46 | } 47 | } 48 | return nil 49 | } 50 | -------------------------------------------------------------------------------- /v3/interface.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | type MapQueryJSON map[string]interface{} 4 | type SliceQueryJSON []interface{} 5 | 6 | type QueryJSONer interface { 7 | QueryJSON(string) (interface{}, bool) 8 | } -------------------------------------------------------------------------------- /v3/interface_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *InterfaceList) Clear() error { 16 | *v = InterfaceList(nil) 17 | return nil 18 | } 19 | 20 | // Validate checks for the values for correctness. If `recurse` 21 | // is specified, child elements are also validated 22 | func (v *InterfaceList) Validate(recurse bool) error { 23 | for i, elem := range *v { 24 | if validator, ok := elem.(Validator); ok { 25 | if err := validator.Validate(recurse); err != nil { 26 | return errors.Wrapf(err, `failed to validate element %d`, i) 27 | } 28 | } 29 | } 30 | return nil 31 | } 32 | -------------------------------------------------------------------------------- /v3/interface_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *InterfaceMap) Clear() error { 16 | *v = make(InterfaceMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in InterfaceMap 21 | func (v *InterfaceMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v InterfaceMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | -------------------------------------------------------------------------------- /v3/internal/cmd/gentypes/gentypes.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/lestrrat-go/openapi/v3/internal/types" 8 | ) 9 | 10 | func main() { 11 | if err := _main(); err != nil { 12 | log.Printf("%s", err) 13 | os.Exit(1) 14 | } 15 | } 16 | 17 | func _main() error { 18 | return types.GenerateCode() 19 | } 20 | -------------------------------------------------------------------------------- /v3/license_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *license) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *license) URL() string { 20 | return v.url 21 | } 22 | 23 | func (v *license) Reference() string { 24 | return v.reference 25 | } 26 | 27 | func (v *license) IsUnresolved() bool { 28 | return v.reference != "" && !v.resolved 29 | } 30 | 31 | func (v *license) Validate(recurse bool) error { 32 | return Visit(context.Background(), newValidator(recurse), v) 33 | } 34 | -------------------------------------------------------------------------------- /v3/license_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // LicenseMutator is used to build an instance of License. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of License with new values 13 | type LicenseMutator struct { 14 | proxy *license 15 | target *license 16 | } 17 | 18 | // Do finalizes the matuation process for License and returns the result 19 | func (b *LicenseMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateLicense creates a new mutator object for License 25 | func MutateLicense(v License) *LicenseMutator { 26 | return &LicenseMutator{ 27 | target: v.(*license), 28 | proxy: v.Clone().(*license), 29 | } 30 | } 31 | 32 | // Name sets the Name field for object License. 33 | func (b *LicenseMutator) Name(v string) *LicenseMutator { 34 | b.proxy.name = v 35 | return b 36 | } 37 | 38 | // URL sets the URL field for object License. 39 | func (b *LicenseMutator) URL(v string) *LicenseMutator { 40 | b.proxy.url = v 41 | return b 42 | } 43 | -------------------------------------------------------------------------------- /v3/license_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // LicenseVisitor is an interface for objects that knows 16 | // how to process License elements while traversing the OpenAPI structure 17 | type LicenseVisitor interface { 18 | VisitLicense(context.Context, License) error 19 | } 20 | 21 | func visitLicense(ctx context.Context, elem License) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(licenseVisitorCtxKey{}).(LicenseVisitor); ok { 29 | if err := v.VisitLicense(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit License element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/link.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (l *link) setName(s string) { 4 | l.name = s 5 | } -------------------------------------------------------------------------------- /v3/link_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *link) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *link) OperationRef() string { 20 | return v.operationRef 21 | } 22 | 23 | func (v *link) OperationID() string { 24 | return v.operationID 25 | } 26 | 27 | func (v *link) Parameters() *InterfaceMapIterator { 28 | var items []interface{} 29 | for key, item := range v.parameters { 30 | items = append(items, &mapIteratorItem{key: key, item: item}) 31 | } 32 | var iter InterfaceMapIterator 33 | iter.list.items = items 34 | return &iter 35 | } 36 | 37 | func (v *link) RequestBody() interface{} { 38 | return v.requestBody 39 | } 40 | 41 | func (v *link) Description() string { 42 | return v.description 43 | } 44 | 45 | func (v *link) Server() Server { 46 | return v.server 47 | } 48 | 49 | func (v *link) Reference() string { 50 | return v.reference 51 | } 52 | 53 | func (v *link) IsUnresolved() bool { 54 | return v.reference != "" && !v.resolved 55 | } 56 | 57 | func (v *link) Validate(recurse bool) error { 58 | return Visit(context.Background(), newValidator(recurse), v) 59 | } 60 | -------------------------------------------------------------------------------- /v3/link_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *LinkMap) Clear() error { 16 | *v = make(LinkMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in LinkMap 21 | func (v *LinkMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v LinkMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *LinkMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*link 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal LinkMap`) 58 | } 59 | tmp := make(map[string]Link) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/link_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // LinkVisitor is an interface for objects that knows 16 | // how to process Link elements while traversing the OpenAPI structure 17 | type LinkVisitor interface { 18 | VisitLink(context.Context, Link) error 19 | } 20 | 21 | func visitLink(ctx context.Context, elem Link) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(linkVisitorCtxKey{}).(LinkVisitor); ok { 29 | if err := v.VisitLink(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Link element`) 34 | } 35 | } 36 | 37 | if child := elem.Server(); child != nil { 38 | if err := visitServer(ctx, child); err != nil { 39 | return errors.Wrap(err, `failed to visit Server element for Link`) 40 | } 41 | } 42 | return nil 43 | } 44 | -------------------------------------------------------------------------------- /v3/media_type.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *mediaType) setMime(s string) { 4 | v.mime = s 5 | } 6 | 7 | func (v *mediaType) setName(s string) { 8 | v.name = s 9 | } 10 | -------------------------------------------------------------------------------- /v3/media_type_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *mediaType) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *mediaType) Mime() string { 20 | return v.mime 21 | } 22 | 23 | func (v *mediaType) Schema() Schema { 24 | return v.schema 25 | } 26 | 27 | func (v *mediaType) Examples() *ExampleMapIterator { 28 | var items []interface{} 29 | for key, item := range v.examples { 30 | items = append(items, &mapIteratorItem{key: key, item: item}) 31 | } 32 | var iter ExampleMapIterator 33 | iter.list.items = items 34 | return &iter 35 | } 36 | 37 | func (v *mediaType) Encoding() *EncodingMapIterator { 38 | var items []interface{} 39 | for key, item := range v.encoding { 40 | items = append(items, &mapIteratorItem{key: key, item: item}) 41 | } 42 | var iter EncodingMapIterator 43 | iter.list.items = items 44 | return &iter 45 | } 46 | 47 | func (v *mediaType) Reference() string { 48 | return v.reference 49 | } 50 | 51 | func (v *mediaType) IsUnresolved() bool { 52 | return v.reference != "" && !v.resolved 53 | } 54 | 55 | func (v *mediaType) Validate(recurse bool) error { 56 | return Visit(context.Background(), newValidator(recurse), v) 57 | } 58 | -------------------------------------------------------------------------------- /v3/media_type_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *MediaTypeMap) Clear() error { 16 | *v = make(MediaTypeMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in MediaTypeMap 21 | func (v *MediaTypeMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v MediaTypeMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *MediaTypeMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*mediaType 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal MediaTypeMap`) 58 | } 59 | tmp := make(map[string]MediaType) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/media_type_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // MediaTypeVisitor is an interface for objects that knows 16 | // how to process MediaType elements while traversing the OpenAPI structure 17 | type MediaTypeVisitor interface { 18 | VisitMediaType(context.Context, MediaType) error 19 | } 20 | 21 | func visitMediaType(ctx context.Context, elem MediaType) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(mediaTypeVisitorCtxKey{}).(MediaTypeVisitor); ok { 29 | if err := v.VisitMediaType(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit MediaType element`) 34 | } 35 | } 36 | 37 | if child := elem.Schema(); child != nil { 38 | if err := visitSchema(ctx, child); err != nil { 39 | return errors.Wrap(err, `failed to visit Schema element for MediaType`) 40 | } 41 | } 42 | return nil 43 | } 44 | -------------------------------------------------------------------------------- /v3/oauth_flow_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *oauthFlow) AuthorizationURL() string { 16 | return v.authorizationURL 17 | } 18 | 19 | func (v *oauthFlow) TokenURL() string { 20 | return v.tokenURL 21 | } 22 | 23 | func (v *oauthFlow) RefreshURL() string { 24 | return v.refreshURL 25 | } 26 | 27 | func (v *oauthFlow) Scopes() *ScopeMapIterator { 28 | var items []interface{} 29 | for key, item := range v.scopes { 30 | items = append(items, &mapIteratorItem{key: key, item: item}) 31 | } 32 | var iter ScopeMapIterator 33 | iter.list.items = items 34 | return &iter 35 | } 36 | 37 | func (v *oauthFlow) Reference() string { 38 | return v.reference 39 | } 40 | 41 | func (v *oauthFlow) IsUnresolved() bool { 42 | return v.reference != "" && !v.resolved 43 | } 44 | 45 | func (v *oauthFlow) Validate(recurse bool) error { 46 | return Visit(context.Background(), newValidator(recurse), v) 47 | } 48 | -------------------------------------------------------------------------------- /v3/oauth_flow_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // OAuthFlowVisitor is an interface for objects that knows 16 | // how to process OAuthFlow elements while traversing the OpenAPI structure 17 | type OAuthFlowVisitor interface { 18 | VisitOAuthFlow(context.Context, OAuthFlow) error 19 | } 20 | 21 | func visitOAuthFlow(ctx context.Context, elem OAuthFlow) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(oauthFlowVisitorCtxKey{}).(OAuthFlowVisitor); ok { 29 | if err := v.VisitOAuthFlow(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit OAuthFlow element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/oauth_flows_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *oauthFlows) Implicit() OAuthFlow { 16 | return v.implicit 17 | } 18 | 19 | func (v *oauthFlows) Password() OAuthFlow { 20 | return v.password 21 | } 22 | 23 | func (v *oauthFlows) ClientCredentials() OAuthFlow { 24 | return v.clientCredentials 25 | } 26 | 27 | func (v *oauthFlows) AuthorizationCode() OAuthFlow { 28 | return v.authorizationCode 29 | } 30 | 31 | func (v *oauthFlows) Reference() string { 32 | return v.reference 33 | } 34 | 35 | func (v *oauthFlows) IsUnresolved() bool { 36 | return v.reference != "" && !v.resolved 37 | } 38 | 39 | func (v *oauthFlows) Validate(recurse bool) error { 40 | return Visit(context.Background(), newValidator(recurse), v) 41 | } 42 | -------------------------------------------------------------------------------- /v3/oauth_flows_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // OAuthFlowsMutator is used to build an instance of OAuthFlows. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of OAuthFlows with new values 13 | type OAuthFlowsMutator struct { 14 | proxy *oauthFlows 15 | target *oauthFlows 16 | } 17 | 18 | // Do finalizes the matuation process for OAuthFlows and returns the result 19 | func (b *OAuthFlowsMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateOAuthFlows creates a new mutator object for OAuthFlows 25 | func MutateOAuthFlows(v OAuthFlows) *OAuthFlowsMutator { 26 | return &OAuthFlowsMutator{ 27 | target: v.(*oauthFlows), 28 | proxy: v.Clone().(*oauthFlows), 29 | } 30 | } 31 | 32 | // Implicit sets the Implicit field for object OAuthFlows. 33 | func (b *OAuthFlowsMutator) Implicit(v OAuthFlow) *OAuthFlowsMutator { 34 | b.proxy.implicit = v 35 | return b 36 | } 37 | 38 | // Password sets the Password field for object OAuthFlows. 39 | func (b *OAuthFlowsMutator) Password(v OAuthFlow) *OAuthFlowsMutator { 40 | b.proxy.password = v 41 | return b 42 | } 43 | 44 | // ClientCredentials sets the ClientCredentials field for object OAuthFlows. 45 | func (b *OAuthFlowsMutator) ClientCredentials(v OAuthFlow) *OAuthFlowsMutator { 46 | b.proxy.clientCredentials = v 47 | return b 48 | } 49 | 50 | // AuthorizationCode sets the AuthorizationCode field for object OAuthFlows. 51 | func (b *OAuthFlowsMutator) AuthorizationCode(v OAuthFlow) *OAuthFlowsMutator { 52 | b.proxy.authorizationCode = v 53 | return b 54 | } 55 | -------------------------------------------------------------------------------- /v3/openapi_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *openAPI) Version() string { 16 | return v.version 17 | } 18 | 19 | func (v *openAPI) Info() Info { 20 | return v.info 21 | } 22 | 23 | func (v *openAPI) Servers() *ServerListIterator { 24 | var items []interface{} 25 | for _, item := range v.servers { 26 | items = append(items, item) 27 | } 28 | var iter ServerListIterator 29 | iter.items = items 30 | return &iter 31 | } 32 | 33 | func (v *openAPI) Paths() Paths { 34 | return v.paths 35 | } 36 | 37 | func (v *openAPI) Components() Components { 38 | return v.components 39 | } 40 | 41 | func (v *openAPI) Security() SecurityRequirement { 42 | return v.security 43 | } 44 | 45 | func (v *openAPI) Tags() *TagListIterator { 46 | var items []interface{} 47 | for _, item := range v.tags { 48 | items = append(items, item) 49 | } 50 | var iter TagListIterator 51 | iter.items = items 52 | return &iter 53 | } 54 | 55 | func (v *openAPI) ExternalDocs() ExternalDocumentation { 56 | return v.externalDocs 57 | } 58 | 59 | func (v *openAPI) Reference() string { 60 | return v.reference 61 | } 62 | 63 | func (v *openAPI) IsUnresolved() bool { 64 | return v.reference != "" && !v.resolved 65 | } 66 | 67 | func (v *openAPI) Validate(recurse bool) error { 68 | return Visit(context.Background(), newValidator(recurse), v) 69 | } 70 | -------------------------------------------------------------------------------- /v3/operation.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *operation) setPathItem(pi PathItem) { 4 | v.pathItem = pi 5 | } 6 | 7 | func (v *operation) setVerb(s string) { 8 | v.verb = s 9 | } 10 | 11 | func (v *operation) Detached() bool { 12 | return v.pathItem == nil 13 | } 14 | 15 | func (v *operation) Path() string { 16 | if v.Detached() { 17 | return "" 18 | } 19 | return v.pathItem.Path() 20 | } 21 | -------------------------------------------------------------------------------- /v3/operation_builder.go: -------------------------------------------------------------------------------- 1 | // +build !gogenerate 2 | 3 | package openapi 4 | 5 | // Tag adds tag `s` to the operation 6 | func (b *OperationBuilder) Tag(s string) *OperationBuilder { 7 | b.target.tags = append(b.target.tags, s) 8 | return b 9 | } 10 | 11 | // Parameter adds parameter `p` to the operation 12 | func (b *OperationBuilder) Parameter(p Parameter) *OperationBuilder { 13 | b.target.parameters = append(b.target.parameters, p.Clone()) 14 | return b 15 | } 16 | 17 | -------------------------------------------------------------------------------- /v3/operation_test.go: -------------------------------------------------------------------------------- 1 | package openapi_test 2 | 3 | import ( 4 | "testing" 5 | 6 | openapi "github.com/lestrrat-go/openapi/v3" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func TestOperationValidate(t *testing.T) { 11 | t.Run("missing responses", func(t *testing.T) { 12 | const src = `{ 13 | "operationId": "HelloWorld", 14 | "description": "A hello world operation" 15 | }` 16 | var oper openapi.Operation 17 | if !assert.NoError(t, openapi.OperationFromJSON([]byte(src), &oper), "unmarshal should not fail") { 18 | return 19 | } 20 | 21 | if !assert.Error(t, oper.Validate(false), "oper.Validate should fail") { 22 | return 23 | } 24 | }) 25 | } 26 | -------------------------------------------------------------------------------- /v3/operation_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // OperationVisitor is an interface for objects that knows 16 | // how to process Operation elements while traversing the OpenAPI structure 17 | type OperationVisitor interface { 18 | VisitOperation(context.Context, Operation) error 19 | } 20 | 21 | func visitOperation(ctx context.Context, elem Operation) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(operationVisitorCtxKey{}).(OperationVisitor); ok { 29 | if err := v.VisitOperation(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Operation element`) 34 | } 35 | } 36 | 37 | if child := elem.ExternalDocs(); child != nil { 38 | if err := visitExternalDocumentation(ctx, child); err != nil { 39 | return errors.Wrap(err, `failed to visit ExternalDocs element for Operation`) 40 | } 41 | } 42 | 43 | if child := elem.RequestBody(); child != nil { 44 | if err := visitRequestBody(ctx, child); err != nil { 45 | return errors.Wrap(err, `failed to visit RequestBody element for Operation`) 46 | } 47 | } 48 | 49 | if child := elem.Responses(); child != nil { 50 | if err := visitResponses(ctx, child); err != nil { 51 | return errors.Wrap(err, `failed to visit Responses element for Operation`) 52 | } 53 | } 54 | return nil 55 | } 56 | -------------------------------------------------------------------------------- /v3/options.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import "github.com/lestrrat-go/openapi/internal/option" 4 | 5 | const ( 6 | optkeyValidate = "validate" 7 | ) 8 | 9 | type Option = option.Interface 10 | 11 | // WithValidate specifies if validation should be performed on the 12 | // object. This option can be passed to `ParseYAML`, `ParseJSON`, 13 | // and `Do` methods for builders and mutators. 14 | func WithValidate(v bool) Option { 15 | return option.New(optkeyValidate, v) 16 | } 17 | -------------------------------------------------------------------------------- /v3/parameter.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *parameter) setName(s string) { 4 | v.name = s 5 | } -------------------------------------------------------------------------------- /v3/parameter_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *ParameterList) Clear() error { 16 | *v = ParameterList(nil) 17 | return nil 18 | } 19 | 20 | // Validate checks for the values for correctness. If `recurse` 21 | // is specified, child elements are also validated 22 | func (v *ParameterList) Validate(recurse bool) error { 23 | for i, elem := range *v { 24 | if validator, ok := elem.(Validator); ok { 25 | if err := validator.Validate(recurse); err != nil { 26 | return errors.Wrapf(err, `failed to validate element %d`, i) 27 | } 28 | } 29 | } 30 | return nil 31 | } 32 | 33 | func (v *ParameterList) UnmarshalJSON(data []byte) error { 34 | var proxy []*parameter 35 | if err := json.Unmarshal(data, &proxy); err != nil { 36 | return errors.Wrap(err, `failed to unmarshal ParameterList`) 37 | } 38 | 39 | if len(proxy) == 0 { 40 | *v = ParameterList(nil) 41 | return nil 42 | } 43 | 44 | tmp := make(ParameterList, len(proxy)) 45 | for i, value := range proxy { 46 | tmp[i] = value 47 | } 48 | *v = tmp 49 | return nil 50 | } 51 | -------------------------------------------------------------------------------- /v3/parameter_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *ParameterMap) Clear() error { 16 | *v = make(ParameterMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in ParameterMap 21 | func (v *ParameterMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v ParameterMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *ParameterMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*parameter 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal ParameterMap`) 58 | } 59 | tmp := make(map[string]Parameter) 60 | for name, value := range proxy { 61 | tmp[name] = value 62 | } 63 | *v = tmp 64 | return nil 65 | } 66 | -------------------------------------------------------------------------------- /v3/parameter_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ParameterVisitor is an interface for objects that knows 16 | // how to process Parameter elements while traversing the OpenAPI structure 17 | type ParameterVisitor interface { 18 | VisitParameter(context.Context, Parameter) error 19 | } 20 | 21 | func visitParameter(ctx context.Context, elem Parameter) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(parameterVisitorCtxKey{}).(ParameterVisitor); ok { 29 | if err := v.VisitParameter(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Parameter element`) 34 | } 35 | } 36 | 37 | if child := elem.Schema(); child != nil { 38 | if err := visitSchema(ctx, child); err != nil { 39 | return errors.Wrap(err, `failed to visit Schema element for Parameter`) 40 | } 41 | } 42 | return nil 43 | } 44 | -------------------------------------------------------------------------------- /v3/path_item.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import "net/http" 4 | 5 | func (v *pathItem) setName(s string) { 6 | v.name = s 7 | } 8 | 9 | func (v *pathItem) postUnmarshalJSON() { 10 | } 11 | 12 | func (v *pathItem) acceptOperation(method string, oper Operation) { 13 | cloned := oper.Clone() 14 | cloned.setVerb(method) 15 | cloned.setPathItem(v) 16 | switch method { 17 | case http.MethodGet: 18 | v.get = cloned 19 | case http.MethodPut: 20 | v.put = cloned 21 | case http.MethodPost: 22 | v.post = cloned 23 | case http.MethodDelete: 24 | v.delete = cloned 25 | case http.MethodOptions: 26 | v.options = cloned 27 | case http.MethodHead: 28 | v.head = cloned 29 | case http.MethodPatch: 30 | v.patch = cloned 31 | case http.MethodTrace: 32 | v.trace = cloned 33 | } 34 | } 35 | 36 | // Operations returns an iterator that you can use to iterate through 37 | // all non-nil operations 38 | func (v *pathItem) Operations() *OperationListIterator { 39 | var items []interface{} 40 | for _, oper := range []Operation{v.get, v.put, v.post, v.delete, v.options, v.head, v.patch, v.trace} { 41 | if oper != nil { 42 | items = append(items, oper) 43 | } 44 | } 45 | 46 | var iter OperationListIterator 47 | iter.items = items 48 | return &iter 49 | } 50 | -------------------------------------------------------------------------------- /v3/path_item_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *PathItemMap) Clear() error { 16 | *v = make(PathItemMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in PathItemMap 21 | func (v *PathItemMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v PathItemMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *PathItemMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*pathItem 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal PathItemMap`) 58 | } 59 | tmp := make(map[string]PathItem) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/path_item_mutator.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import "net/http" 4 | 5 | func (m *PathItemMutator) Get(v Operation) *PathItemMutator { 6 | m.proxy.acceptOperation(http.MethodGet, v) 7 | return m 8 | } 9 | 10 | func (m *PathItemMutator) Put(v Operation) *PathItemMutator { 11 | m.proxy.acceptOperation(http.MethodPut, v) 12 | return m 13 | } 14 | 15 | func (m *PathItemMutator) Post(v Operation) *PathItemMutator { 16 | m.proxy.acceptOperation(http.MethodPost, v) 17 | return m 18 | } 19 | 20 | func (m *PathItemMutator) Delete(v Operation) *PathItemMutator { 21 | m.proxy.acceptOperation(http.MethodDelete, v) 22 | return m 23 | } 24 | 25 | func (m *PathItemMutator) Options(v Operation) *PathItemMutator { 26 | m.proxy.acceptOperation(http.MethodOptions, v) 27 | return m 28 | } 29 | 30 | func (m *PathItemMutator) Head(v Operation) *PathItemMutator { 31 | m.proxy.acceptOperation(http.MethodHead, v) 32 | return m 33 | } 34 | 35 | func (m *PathItemMutator) Patch(v Operation) *PathItemMutator { 36 | m.proxy.acceptOperation(http.MethodPatch, v) 37 | return m 38 | } 39 | 40 | func (m *PathItemMutator) Trace(v Operation) *PathItemMutator { 41 | m.proxy.acceptOperation(http.MethodTrace, v) 42 | return m 43 | } 44 | -------------------------------------------------------------------------------- /v3/paths.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "sort" 7 | "strconv" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | func (p *paths) MarshalJSON() ([]byte, error) { 13 | if p == nil { 14 | return []byte("null"), nil 15 | } 16 | 17 | var keys []string 18 | for key := range p.paths { 19 | keys = append(keys, key) 20 | } 21 | 22 | sort.Strings(keys) 23 | 24 | var buf bytes.Buffer 25 | buf.WriteByte('{') 26 | for i, key := range keys { 27 | buf.WriteString(strconv.Quote(key)) 28 | buf.WriteByte(':') 29 | if err := json.NewEncoder(&buf).Encode(p.paths[key]); err != nil { 30 | return nil, errors.Wrapf(err, `failed to encode paths.%s`, key) 31 | } 32 | 33 | if i < len(keys)-1 { 34 | buf.WriteByte(',') 35 | } 36 | } 37 | buf.WriteByte('}') 38 | return buf.Bytes(), nil 39 | } 40 | 41 | func (p *paths) UnmarshalJSON(data []byte) error { 42 | var m map[string]json.RawMessage 43 | if err := json.Unmarshal(data, &m); err != nil { 44 | return errors.Wrap(err, `failed to unmarshal JSON`) 45 | } 46 | 47 | tmp := paths{ 48 | paths: make(map[string]PathItem), 49 | } 50 | 51 | mutator := MutatePaths(&tmp) 52 | 53 | for path, data := range m { 54 | var pi pathItem 55 | if err := json.Unmarshal(data, &pi); err != nil { 56 | return errors.Wrap(err, `failed to unmarshal JSON`) 57 | } 58 | 59 | mutator.Path(path, &pi) 60 | } 61 | 62 | if err := mutator.Do(); err != nil { 63 | return errors.Wrap(err, `failed to mutate path`) 64 | } 65 | 66 | *p = tmp 67 | return nil 68 | } 69 | -------------------------------------------------------------------------------- /v3/paths_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *paths) Paths() *PathItemMapIterator { 16 | var items []interface{} 17 | for key, item := range v.paths { 18 | items = append(items, &mapIteratorItem{key: key, item: item}) 19 | } 20 | var iter PathItemMapIterator 21 | iter.list.items = items 22 | return &iter 23 | } 24 | 25 | func (v *paths) Reference() string { 26 | return v.reference 27 | } 28 | 29 | func (v *paths) IsUnresolved() bool { 30 | return v.reference != "" && !v.resolved 31 | } 32 | 33 | func (v *paths) Validate(recurse bool) error { 34 | return Visit(context.Background(), newValidator(recurse), v) 35 | } 36 | -------------------------------------------------------------------------------- /v3/paths_builder.go: -------------------------------------------------------------------------------- 1 | // +build !gogenerate 2 | 3 | package openapi 4 | 5 | func (v *paths) addPathItem(path string, item PathItem) { 6 | if v.paths == nil { 7 | v.paths = make(map[string]PathItem) 8 | } 9 | 10 | v.paths[path] = item.Clone() 11 | v.paths[path].setPath(path) 12 | } 13 | 14 | // Path sets the path item for path `path` to `item` 15 | func (b *PathsBuilder) Path(path string, item PathItem) *PathsBuilder { 16 | b.target.addPathItem(path, item) 17 | return b 18 | } 19 | 20 | // Path sets the path item for path `path` to `item` 21 | func (m *PathsMutator) Path(path string, item PathItem) *PathsMutator { 22 | m.proxy.addPathItem(path, item) 23 | return m 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /v3/paths_builder_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "github.com/pkg/errors" 7 | 8 | var _ = errors.Cause 9 | 10 | // PathsBuilder is used to build an instance of Paths. The user must 11 | // call `Build()` after providing all the necessary information to 12 | // build an instance of Paths 13 | type PathsBuilder struct { 14 | target *paths 15 | } 16 | 17 | // MustBuild is a convenience function for those time when you know that 18 | // the result of the builder must be successful 19 | func (b *PathsBuilder) MustBuild(options ...Option) Paths { 20 | v, err := b.Build() 21 | if err != nil { 22 | panic(err) 23 | } 24 | return v 25 | } 26 | 27 | // Build finalizes the building process for Paths and returns the result 28 | func (b *PathsBuilder) Build(options ...Option) (Paths, error) { 29 | validate := true 30 | for _, option := range options { 31 | switch option.Name() { 32 | case optkeyValidate: 33 | validate = option.Value().(bool) 34 | } 35 | } 36 | if validate { 37 | if err := b.target.Validate(false); err != nil { 38 | return nil, errors.Wrap(err, `validation failed`) 39 | } 40 | } 41 | return b.target, nil 42 | } 43 | 44 | // NewPaths creates a new builder object for Paths 45 | func NewPaths() *PathsBuilder { 46 | return &PathsBuilder{ 47 | target: &paths{}, 48 | } 49 | } 50 | 51 | // Paths sets the Paths field for object Paths. 52 | func (b *PathsBuilder) Paths(v map[string]PathItem) *PathsBuilder { 53 | b.target.paths = v 54 | return b 55 | } 56 | 57 | // Reference sets the $ref (reference) field for object Paths. 58 | func (b *PathsBuilder) Reference(v string) *PathsBuilder { 59 | b.target.reference = v 60 | return b 61 | } 62 | -------------------------------------------------------------------------------- /v3/paths_json_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | "log" 9 | "strings" 10 | 11 | "github.com/pkg/errors" 12 | ) 13 | 14 | var _ = log.Printf 15 | var _ = json.Unmarshal 16 | var _ = errors.Cause 17 | 18 | func (v *paths) QueryJSON(path string) (ret interface{}, ok bool) { 19 | path = strings.TrimLeftFunc(path, func(r rune) bool { return r == '#' || r == '/' }) 20 | if path == "" { 21 | return v, true 22 | } 23 | 24 | var frag string 25 | if i := strings.Index(path, "/"); i > -1 { 26 | frag = path[:i] 27 | path = path[i+1:] 28 | } else { 29 | frag = path 30 | path = "" 31 | } 32 | 33 | var target interface{} 34 | 35 | switch frag { 36 | default: 37 | return nil, false 38 | } 39 | 40 | if qj, ok := target.(QueryJSONer); ok { 41 | return qj.QueryJSON(path) 42 | } 43 | if path == "" { 44 | return target, true 45 | } 46 | return nil, false 47 | } 48 | 49 | // PathsFromJSON constructs a Paths from JSON buffer. `dst` must 50 | // be a pointer to `Paths` 51 | func PathsFromJSON(buf []byte, dst interface{}) error { 52 | v, ok := dst.(*Paths) 53 | if !ok { 54 | return errors.Errorf(`dst needs to be a pointer to Paths, but got %T`, dst) 55 | } 56 | var tmp paths 57 | if err := json.Unmarshal(buf, &tmp); err != nil { 58 | return errors.Wrap(err, `failed to unmarshal Paths`) 59 | } 60 | *v = &tmp 61 | return nil 62 | } 63 | -------------------------------------------------------------------------------- /v3/paths_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // PathsMutator is used to build an instance of Paths. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of Paths with new values 13 | type PathsMutator struct { 14 | proxy *paths 15 | target *paths 16 | } 17 | 18 | // Do finalizes the matuation process for Paths and returns the result 19 | func (b *PathsMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutatePaths creates a new mutator object for Paths 25 | func MutatePaths(v Paths) *PathsMutator { 26 | return &PathsMutator{ 27 | target: v.(*paths), 28 | proxy: v.Clone().(*paths), 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /v3/paths_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // PathsVisitor is an interface for objects that knows 16 | // how to process Paths elements while traversing the OpenAPI structure 17 | type PathsVisitor interface { 18 | VisitPaths(context.Context, Paths) error 19 | } 20 | 21 | func visitPaths(ctx context.Context, elem Paths) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(pathsVisitorCtxKey{}).(PathsVisitor); ok { 29 | if err := v.VisitPaths(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Paths element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/request_body.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *requestBody) setName(s string) { 4 | v.name = s 5 | } 6 | 7 | func (v *requestBody) postUnmarshalJSON() { 8 | for mime, content := range v.content { 9 | content.setMime(mime) 10 | } 11 | } 12 | 13 | func (v *requestBody) Contents() *MediaTypeListIterator { 14 | var items []interface{} 15 | for _, mt := range v.content { 16 | items = append(items, mt) 17 | } 18 | 19 | var iter MediaTypeListIterator 20 | iter.items = items 21 | return &iter 22 | } 23 | -------------------------------------------------------------------------------- /v3/request_body_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *requestBody) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *requestBody) Description() string { 20 | return v.description 21 | } 22 | 23 | func (v *requestBody) Content() *MediaTypeMapIterator { 24 | var items []interface{} 25 | for key, item := range v.content { 26 | items = append(items, &mapIteratorItem{key: key, item: item}) 27 | } 28 | var iter MediaTypeMapIterator 29 | iter.list.items = items 30 | return &iter 31 | } 32 | 33 | func (v *requestBody) Required() bool { 34 | return v.required 35 | } 36 | 37 | func (v *requestBody) Reference() string { 38 | return v.reference 39 | } 40 | 41 | func (v *requestBody) IsUnresolved() bool { 42 | return v.reference != "" && !v.resolved 43 | } 44 | 45 | func (v *requestBody) Validate(recurse bool) error { 46 | return Visit(context.Background(), newValidator(recurse), v) 47 | } 48 | -------------------------------------------------------------------------------- /v3/request_body_builder.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // Content sets the Content field for object RequestBody. 4 | func (b *RequestBodyBuilder) Content(mime MediaTypeMapKey, mt MediaType) *RequestBodyBuilder { 5 | b.target.content[mime] = mt.Clone() 6 | b.target.content[mime].setMime(mime) 7 | return b 8 | } 9 | 10 | 11 | // Content sets the Content field for object RequestBody. 12 | func (b *RequestBodyMutator) Content(mime MediaTypeMapKey, mt MediaType) *RequestBodyMutator { 13 | b.proxy.content[mime] = mt.Clone() 14 | b.proxy.content[mime].setMime(mime) 15 | return b 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /v3/request_body_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *RequestBodyMap) Clear() error { 16 | *v = make(RequestBodyMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in RequestBodyMap 21 | func (v *RequestBodyMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v RequestBodyMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *RequestBodyMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*requestBody 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal RequestBodyMap`) 58 | } 59 | tmp := make(map[string]RequestBody) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/request_body_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // RequestBodyMutator is used to build an instance of RequestBody. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of RequestBody with new values 13 | type RequestBodyMutator struct { 14 | proxy *requestBody 15 | target *requestBody 16 | } 17 | 18 | // Do finalizes the matuation process for RequestBody and returns the result 19 | func (b *RequestBodyMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateRequestBody creates a new mutator object for RequestBody 25 | func MutateRequestBody(v RequestBody) *RequestBodyMutator { 26 | return &RequestBodyMutator{ 27 | target: v.(*requestBody), 28 | proxy: v.Clone().(*requestBody), 29 | } 30 | } 31 | 32 | // Name sets the Name field for object RequestBody. 33 | func (b *RequestBodyMutator) Name(v string) *RequestBodyMutator { 34 | b.proxy.name = v 35 | return b 36 | } 37 | 38 | // Description sets the Description field for object RequestBody. 39 | func (b *RequestBodyMutator) Description(v string) *RequestBodyMutator { 40 | b.proxy.description = v 41 | return b 42 | } 43 | 44 | // Required sets the Required field for object RequestBody. 45 | func (b *RequestBodyMutator) Required(v bool) *RequestBodyMutator { 46 | b.proxy.required = v 47 | return b 48 | } 49 | -------------------------------------------------------------------------------- /v3/request_body_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // RequestBodyVisitor is an interface for objects that knows 16 | // how to process RequestBody elements while traversing the OpenAPI structure 17 | type RequestBodyVisitor interface { 18 | VisitRequestBody(context.Context, RequestBody) error 19 | } 20 | 21 | func visitRequestBody(ctx context.Context, elem RequestBody) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(requestBodyVisitorCtxKey{}).(RequestBodyVisitor); ok { 29 | if err := v.VisitRequestBody(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit RequestBody element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/resolver.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import ( 4 | "errors" 5 | "sync" 6 | ) 7 | 8 | type Resolver struct { 9 | mu sync.RWMutex 10 | cache map[string]interface{} 11 | ctx OpenAPI 12 | } 13 | 14 | func NewResolver(ctx OpenAPI) *Resolver { 15 | return &Resolver{ 16 | cache: make(map[string]interface{}), 17 | ctx: ctx, 18 | } 19 | } 20 | 21 | func (r *Resolver) Resolve(path string) (interface{}, error) { 22 | r.mu.RLock() 23 | if v, ok := r.cache[path]; ok { 24 | defer r.mu.RUnlock() 25 | return v, nil 26 | } 27 | r.mu.RUnlock() 28 | 29 | v, ok := r.ctx.QueryJSON(path) 30 | if !ok { 31 | return nil, errors.New(`could not resolve reference`) 32 | } 33 | 34 | r.mu.Lock() 35 | r.cache[path] = v 36 | r.mu.Unlock() 37 | return v, nil 38 | } 39 | -------------------------------------------------------------------------------- /v3/response.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *response) setName(s string) { 4 | v.name = s 5 | } -------------------------------------------------------------------------------- /v3/response_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *response) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *response) Description() string { 20 | return v.description 21 | } 22 | 23 | func (v *response) Headers() *HeaderMapIterator { 24 | var items []interface{} 25 | for key, item := range v.headers { 26 | items = append(items, &mapIteratorItem{key: key, item: item}) 27 | } 28 | var iter HeaderMapIterator 29 | iter.list.items = items 30 | return &iter 31 | } 32 | 33 | func (v *response) Content() *MediaTypeMapIterator { 34 | var items []interface{} 35 | for key, item := range v.content { 36 | items = append(items, &mapIteratorItem{key: key, item: item}) 37 | } 38 | var iter MediaTypeMapIterator 39 | iter.list.items = items 40 | return &iter 41 | } 42 | 43 | func (v *response) Links() *LinkMapIterator { 44 | var items []interface{} 45 | for key, item := range v.links { 46 | items = append(items, &mapIteratorItem{key: key, item: item}) 47 | } 48 | var iter LinkMapIterator 49 | iter.list.items = items 50 | return &iter 51 | } 52 | 53 | func (v *response) Reference() string { 54 | return v.reference 55 | } 56 | 57 | func (v *response) IsUnresolved() bool { 58 | return v.reference != "" && !v.resolved 59 | } 60 | 61 | func (v *response) Validate(recurse bool) error { 62 | return Visit(context.Background(), newValidator(recurse), v) 63 | } 64 | -------------------------------------------------------------------------------- /v3/response_builder.go: -------------------------------------------------------------------------------- 1 | // +build !gogenerate 2 | 3 | package openapi 4 | 5 | // Header sets the header `name` to `hdr` 6 | func (b *ResponseBuilder) Header(name string, hdr Header) *ResponseBuilder { 7 | if b.target.headers == nil { 8 | b.target.headers = make(map[string]Header) 9 | } 10 | 11 | hdr = hdr.Clone() 12 | b.target.headers[name] = hdr 13 | hdr.setName(name) 14 | return b 15 | } 16 | 17 | // Content sets th content type `mime` to `desc` 18 | func (b *ResponseBuilder) Content(mime string, desc MediaType) *ResponseBuilder { 19 | if b.target.content == nil { 20 | b.target.content = make(map[string]MediaType) 21 | } 22 | 23 | desc = desc.Clone() 24 | b.target.content[mime] = desc 25 | desc.setMime(mime) 26 | return b 27 | } 28 | -------------------------------------------------------------------------------- /v3/response_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *ResponseMap) Clear() error { 16 | *v = make(ResponseMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in ResponseMap 21 | func (v *ResponseMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v ResponseMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *ResponseMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*response 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal ResponseMap`) 58 | } 59 | tmp := make(map[string]Response) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/response_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ResponseVisitor is an interface for objects that knows 16 | // how to process Response elements while traversing the OpenAPI structure 17 | type ResponseVisitor interface { 18 | VisitResponse(context.Context, Response) error 19 | } 20 | 21 | func visitResponse(ctx context.Context, elem Response) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(responseVisitorCtxKey{}).(ResponseVisitor); ok { 29 | if err := v.VisitResponse(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Response element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/responses_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *responses) Default() Response { 16 | return v.defaultValue 17 | } 18 | 19 | func (v *responses) Responses() *ResponseMapIterator { 20 | var items []interface{} 21 | for key, item := range v.responses { 22 | items = append(items, &mapIteratorItem{key: key, item: item}) 23 | } 24 | var iter ResponseMapIterator 25 | iter.list.items = items 26 | return &iter 27 | } 28 | 29 | func (v *responses) Reference() string { 30 | return v.reference 31 | } 32 | 33 | func (v *responses) IsUnresolved() bool { 34 | return v.reference != "" && !v.resolved 35 | } 36 | 37 | func (v *responses) Validate(recurse bool) error { 38 | return Visit(context.Background(), newValidator(recurse), v) 39 | } 40 | -------------------------------------------------------------------------------- /v3/responses_builder.go: -------------------------------------------------------------------------------- 1 | // +build !gogenerate 2 | 3 | package openapi 4 | 5 | // Response sets the response for status code `code` to `v` 6 | func (b *ResponsesBuilder) Response(code string, v Response) *ResponsesBuilder { 7 | if b.target.responses == nil { 8 | b.target.responses = make(map[string]Response) 9 | } 10 | b.target.responses[code] = v.Clone() 11 | return b 12 | } 13 | 14 | -------------------------------------------------------------------------------- /v3/responses_json_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | "log" 9 | "strings" 10 | 11 | "github.com/pkg/errors" 12 | ) 13 | 14 | var _ = log.Printf 15 | var _ = json.Unmarshal 16 | var _ = errors.Cause 17 | 18 | func (v *responses) QueryJSON(path string) (ret interface{}, ok bool) { 19 | path = strings.TrimLeftFunc(path, func(r rune) bool { return r == '#' || r == '/' }) 20 | if path == "" { 21 | return v, true 22 | } 23 | 24 | var frag string 25 | if i := strings.Index(path, "/"); i > -1 { 26 | frag = path[:i] 27 | path = path[i+1:] 28 | } else { 29 | frag = path 30 | path = "" 31 | } 32 | 33 | var target interface{} 34 | 35 | switch frag { 36 | case "default": 37 | target = v.defaultValue 38 | default: 39 | return nil, false 40 | } 41 | 42 | if qj, ok := target.(QueryJSONer); ok { 43 | return qj.QueryJSON(path) 44 | } 45 | if path == "" { 46 | return target, true 47 | } 48 | return nil, false 49 | } 50 | 51 | // ResponsesFromJSON constructs a Responses from JSON buffer. `dst` must 52 | // be a pointer to `Responses` 53 | func ResponsesFromJSON(buf []byte, dst interface{}) error { 54 | v, ok := dst.(*Responses) 55 | if !ok { 56 | return errors.Errorf(`dst needs to be a pointer to Responses, but got %T`, dst) 57 | } 58 | var tmp responses 59 | if err := json.Unmarshal(buf, &tmp); err != nil { 60 | return errors.Wrap(err, `failed to unmarshal Responses`) 61 | } 62 | *v = &tmp 63 | return nil 64 | } 65 | -------------------------------------------------------------------------------- /v3/responses_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // ResponsesMutator is used to build an instance of Responses. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of Responses with new values 13 | type ResponsesMutator struct { 14 | proxy *responses 15 | target *responses 16 | } 17 | 18 | // Do finalizes the matuation process for Responses and returns the result 19 | func (b *ResponsesMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateResponses creates a new mutator object for Responses 25 | func MutateResponses(v Responses) *ResponsesMutator { 26 | return &ResponsesMutator{ 27 | target: v.(*responses), 28 | proxy: v.Clone().(*responses), 29 | } 30 | } 31 | 32 | // Default sets the Default field for object Responses. 33 | func (b *ResponsesMutator) Default(v Response) *ResponsesMutator { 34 | b.proxy.defaultValue = v 35 | return b 36 | } 37 | 38 | func (b *ResponsesMutator) ClearResponses() *ResponsesMutator { 39 | b.proxy.responses.Clear() 40 | return b 41 | } 42 | 43 | func (b *ResponsesMutator) Response(key ResponseMapKey, value Response) *ResponsesMutator { 44 | if b.proxy.responses == nil { 45 | b.proxy.responses = ResponseMap{} 46 | } 47 | 48 | b.proxy.responses[key] = value 49 | return b 50 | } 51 | -------------------------------------------------------------------------------- /v3/responses_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ResponsesVisitor is an interface for objects that knows 16 | // how to process Responses elements while traversing the OpenAPI structure 17 | type ResponsesVisitor interface { 18 | VisitResponses(context.Context, Responses) error 19 | } 20 | 21 | func visitResponses(ctx context.Context, elem Responses) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(responsesVisitorCtxKey{}).(ResponsesVisitor); ok { 29 | if err := v.VisitResponses(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Responses element`) 34 | } 35 | } 36 | 37 | if child := elem.Default(); child != nil { 38 | if err := visitResponse(ctx, child); err != nil { 39 | return errors.Wrap(err, `failed to visit Default element for Responses`) 40 | } 41 | } 42 | return nil 43 | } 44 | -------------------------------------------------------------------------------- /v3/schema.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *schema) setName(s string) { 4 | v.name = s 5 | } 6 | 7 | func (v *schema) Type() PrimitiveType { 8 | if v.typ != "" { 9 | return v.typ 10 | } 11 | 12 | // Guess 13 | if len(v.properties) > 0 { 14 | return Object 15 | } 16 | 17 | if v.items != nil { 18 | return Array 19 | } 20 | 21 | return Invalid 22 | } 23 | -------------------------------------------------------------------------------- /v3/schema_builder.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // Property sets property `name` to `s` 4 | func (b *SchemaBuilder) Property(name string, s Schema) *SchemaBuilder { 5 | if b.target.properties == nil { 6 | b.target.properties = make(map[string]Schema) 7 | } 8 | s = s.Clone() 9 | b.target.properties[name] = s 10 | s.setName(name) 11 | return b 12 | } 13 | -------------------------------------------------------------------------------- /v3/schema_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *SchemaList) Clear() error { 16 | *v = SchemaList(nil) 17 | return nil 18 | } 19 | 20 | // Validate checks for the values for correctness. If `recurse` 21 | // is specified, child elements are also validated 22 | func (v *SchemaList) Validate(recurse bool) error { 23 | for i, elem := range *v { 24 | if validator, ok := elem.(Validator); ok { 25 | if err := validator.Validate(recurse); err != nil { 26 | return errors.Wrapf(err, `failed to validate element %d`, i) 27 | } 28 | } 29 | } 30 | return nil 31 | } 32 | 33 | func (v *SchemaList) UnmarshalJSON(data []byte) error { 34 | var proxy []*schema 35 | if err := json.Unmarshal(data, &proxy); err != nil { 36 | return errors.Wrap(err, `failed to unmarshal SchemaList`) 37 | } 38 | 39 | if len(proxy) == 0 { 40 | *v = SchemaList(nil) 41 | return nil 42 | } 43 | 44 | tmp := make(SchemaList, len(proxy)) 45 | for i, value := range proxy { 46 | tmp[i] = value 47 | } 48 | *v = tmp 49 | return nil 50 | } 51 | -------------------------------------------------------------------------------- /v3/schema_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *SchemaMap) Clear() error { 16 | *v = make(SchemaMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in SchemaMap 21 | func (v *SchemaMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v SchemaMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *SchemaMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*schema 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal SchemaMap`) 58 | } 59 | tmp := make(map[string]Schema) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/scope_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *ScopeMap) Clear() error { 16 | *v = make(ScopeMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in ScopeMap 21 | func (v *ScopeMap) Validate(recurse bool) error { 22 | return nil 23 | } 24 | 25 | func (v ScopeMap) QueryJSON(path string) (ret interface{}, ok bool) { 26 | if path == `` { 27 | return v, true 28 | } 29 | 30 | var frag string 31 | frag, path = extractFragFromPath(path) 32 | target, ok := v[frag] 33 | if !ok { 34 | return nil, false 35 | } 36 | 37 | if path == `` { 38 | return target, true 39 | } 40 | return nil, false 41 | } 42 | -------------------------------------------------------------------------------- /v3/security_requirement_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *securityRequirement) Schemes() *StringListMapIterator { 16 | var items []interface{} 17 | for key, item := range v.schemes { 18 | items = append(items, &mapIteratorItem{key: key, item: item}) 19 | } 20 | var iter StringListMapIterator 21 | iter.list.items = items 22 | return &iter 23 | } 24 | 25 | func (v *securityRequirement) Reference() string { 26 | return v.reference 27 | } 28 | 29 | func (v *securityRequirement) IsUnresolved() bool { 30 | return v.reference != "" && !v.resolved 31 | } 32 | 33 | func (v *securityRequirement) Validate(recurse bool) error { 34 | return Visit(context.Background(), newValidator(recurse), v) 35 | } 36 | -------------------------------------------------------------------------------- /v3/security_requirement_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *SecurityRequirementList) Clear() error { 16 | *v = SecurityRequirementList(nil) 17 | return nil 18 | } 19 | 20 | // Validate checks for the values for correctness. If `recurse` 21 | // is specified, child elements are also validated 22 | func (v *SecurityRequirementList) Validate(recurse bool) error { 23 | for i, elem := range *v { 24 | if validator, ok := elem.(Validator); ok { 25 | if err := validator.Validate(recurse); err != nil { 26 | return errors.Wrapf(err, `failed to validate element %d`, i) 27 | } 28 | } 29 | } 30 | return nil 31 | } 32 | 33 | func (v *SecurityRequirementList) UnmarshalJSON(data []byte) error { 34 | var proxy []*securityRequirement 35 | if err := json.Unmarshal(data, &proxy); err != nil { 36 | return errors.Wrap(err, `failed to unmarshal SecurityRequirementList`) 37 | } 38 | 39 | if len(proxy) == 0 { 40 | *v = SecurityRequirementList(nil) 41 | return nil 42 | } 43 | 44 | tmp := make(SecurityRequirementList, len(proxy)) 45 | for i, value := range proxy { 46 | tmp[i] = value 47 | } 48 | *v = tmp 49 | return nil 50 | } 51 | -------------------------------------------------------------------------------- /v3/security_requirement_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // SecurityRequirementMutator is used to build an instance of SecurityRequirement. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of SecurityRequirement with new values 13 | type SecurityRequirementMutator struct { 14 | proxy *securityRequirement 15 | target *securityRequirement 16 | } 17 | 18 | // Do finalizes the matuation process for SecurityRequirement and returns the result 19 | func (b *SecurityRequirementMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateSecurityRequirement creates a new mutator object for SecurityRequirement 25 | func MutateSecurityRequirement(v SecurityRequirement) *SecurityRequirementMutator { 26 | return &SecurityRequirementMutator{ 27 | target: v.(*securityRequirement), 28 | proxy: v.Clone().(*securityRequirement), 29 | } 30 | } 31 | 32 | func (b *SecurityRequirementMutator) ClearSchemes() *SecurityRequirementMutator { 33 | b.proxy.schemes.Clear() 34 | return b 35 | } 36 | 37 | func (b *SecurityRequirementMutator) Scheme(key StringListMapKey, value []string) *SecurityRequirementMutator { 38 | if b.proxy.schemes == nil { 39 | b.proxy.schemes = StringListMap{} 40 | } 41 | 42 | b.proxy.schemes[key] = value 43 | return b 44 | } 45 | -------------------------------------------------------------------------------- /v3/security_requirement_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // SecurityRequirementVisitor is an interface for objects that knows 16 | // how to process SecurityRequirement elements while traversing the OpenAPI structure 17 | type SecurityRequirementVisitor interface { 18 | VisitSecurityRequirement(context.Context, SecurityRequirement) error 19 | } 20 | 21 | func visitSecurityRequirement(ctx context.Context, elem SecurityRequirement) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(securityRequirementVisitorCtxKey{}).(SecurityRequirementVisitor); ok { 29 | if err := v.VisitSecurityRequirement(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit SecurityRequirement element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/security_scheme_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *securityScheme) Type() string { 16 | return v.typ 17 | } 18 | 19 | func (v *securityScheme) Description() string { 20 | return v.description 21 | } 22 | 23 | func (v *securityScheme) Name() string { 24 | return v.name 25 | } 26 | 27 | func (v *securityScheme) In() string { 28 | return v.in 29 | } 30 | 31 | func (v *securityScheme) Scheme() string { 32 | return v.scheme 33 | } 34 | 35 | func (v *securityScheme) BearerFormat() string { 36 | return v.bearerFormat 37 | } 38 | 39 | func (v *securityScheme) Flows() OAuthFlows { 40 | return v.flows 41 | } 42 | 43 | func (v *securityScheme) OpenIDConnectURL() string { 44 | return v.openIDConnectURL 45 | } 46 | 47 | func (v *securityScheme) Reference() string { 48 | return v.reference 49 | } 50 | 51 | func (v *securityScheme) IsUnresolved() bool { 52 | return v.reference != "" && !v.resolved 53 | } 54 | 55 | func (v *securityScheme) Validate(recurse bool) error { 56 | return Visit(context.Background(), newValidator(recurse), v) 57 | } 58 | -------------------------------------------------------------------------------- /v3/security_scheme_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *SecuritySchemeMap) Clear() error { 16 | *v = make(SecuritySchemeMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in SecuritySchemeMap 21 | func (v *SecuritySchemeMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v SecuritySchemeMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *SecuritySchemeMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*securityScheme 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal SecuritySchemeMap`) 58 | } 59 | tmp := make(map[string]SecurityScheme) 60 | for name, value := range proxy { 61 | tmp[name] = value 62 | } 63 | *v = tmp 64 | return nil 65 | } 66 | -------------------------------------------------------------------------------- /v3/security_scheme_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // SecuritySchemeVisitor is an interface for objects that knows 16 | // how to process SecurityScheme elements while traversing the OpenAPI structure 17 | type SecuritySchemeVisitor interface { 18 | VisitSecurityScheme(context.Context, SecurityScheme) error 19 | } 20 | 21 | func visitSecurityScheme(ctx context.Context, elem SecurityScheme) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(securitySchemeVisitorCtxKey{}).(SecuritySchemeVisitor); ok { 29 | if err := v.VisitSecurityScheme(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit SecurityScheme element`) 34 | } 35 | } 36 | 37 | if child := elem.Flows(); child != nil { 38 | if err := visitOAuthFlows(ctx, child); err != nil { 39 | return errors.Wrap(err, `failed to visit Flows element for SecurityScheme`) 40 | } 41 | } 42 | return nil 43 | } 44 | -------------------------------------------------------------------------------- /v3/server_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *server) URL() string { 16 | return v.url 17 | } 18 | 19 | func (v *server) Description() string { 20 | return v.description 21 | } 22 | 23 | func (v *server) Variables() *ServerVariableMapIterator { 24 | var items []interface{} 25 | for key, item := range v.variables { 26 | items = append(items, &mapIteratorItem{key: key, item: item}) 27 | } 28 | var iter ServerVariableMapIterator 29 | iter.list.items = items 30 | return &iter 31 | } 32 | 33 | func (v *server) Reference() string { 34 | return v.reference 35 | } 36 | 37 | func (v *server) IsUnresolved() bool { 38 | return v.reference != "" && !v.resolved 39 | } 40 | 41 | func (v *server) Validate(recurse bool) error { 42 | return Visit(context.Background(), newValidator(recurse), v) 43 | } 44 | -------------------------------------------------------------------------------- /v3/server_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *ServerList) Clear() error { 16 | *v = ServerList(nil) 17 | return nil 18 | } 19 | 20 | // Validate checks for the values for correctness. If `recurse` 21 | // is specified, child elements are also validated 22 | func (v *ServerList) Validate(recurse bool) error { 23 | for i, elem := range *v { 24 | if validator, ok := elem.(Validator); ok { 25 | if err := validator.Validate(recurse); err != nil { 26 | return errors.Wrapf(err, `failed to validate element %d`, i) 27 | } 28 | } 29 | } 30 | return nil 31 | } 32 | 33 | func (v *ServerList) UnmarshalJSON(data []byte) error { 34 | var proxy []*server 35 | if err := json.Unmarshal(data, &proxy); err != nil { 36 | return errors.Wrap(err, `failed to unmarshal ServerList`) 37 | } 38 | 39 | if len(proxy) == 0 { 40 | *v = ServerList(nil) 41 | return nil 42 | } 43 | 44 | tmp := make(ServerList, len(proxy)) 45 | for i, value := range proxy { 46 | tmp[i] = value 47 | } 48 | *v = tmp 49 | return nil 50 | } 51 | -------------------------------------------------------------------------------- /v3/server_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // ServerMutator is used to build an instance of Server. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of Server with new values 13 | type ServerMutator struct { 14 | proxy *server 15 | target *server 16 | } 17 | 18 | // Do finalizes the matuation process for Server and returns the result 19 | func (b *ServerMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateServer creates a new mutator object for Server 25 | func MutateServer(v Server) *ServerMutator { 26 | return &ServerMutator{ 27 | target: v.(*server), 28 | proxy: v.Clone().(*server), 29 | } 30 | } 31 | 32 | // URL sets the URL field for object Server. 33 | func (b *ServerMutator) URL(v string) *ServerMutator { 34 | b.proxy.url = v 35 | return b 36 | } 37 | 38 | // Description sets the Description field for object Server. 39 | func (b *ServerMutator) Description(v string) *ServerMutator { 40 | b.proxy.description = v 41 | return b 42 | } 43 | 44 | func (b *ServerMutator) ClearVariables() *ServerMutator { 45 | b.proxy.variables.Clear() 46 | return b 47 | } 48 | 49 | func (b *ServerMutator) Variable(key ServerVariableMapKey, value ServerVariable) *ServerMutator { 50 | if b.proxy.variables == nil { 51 | b.proxy.variables = ServerVariableMap{} 52 | } 53 | 54 | b.proxy.variables[key] = value 55 | return b 56 | } 57 | -------------------------------------------------------------------------------- /v3/server_variable.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | func (v *serverVariable) setName(s string) { 4 | v.name = s 5 | } -------------------------------------------------------------------------------- /v3/server_variable_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *serverVariable) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *serverVariable) Enum() *StringListIterator { 20 | var items []interface{} 21 | for _, item := range v.enum { 22 | items = append(items, item) 23 | } 24 | var iter StringListIterator 25 | iter.items = items 26 | return &iter 27 | } 28 | 29 | func (v *serverVariable) Default() string { 30 | return v.defaultValue 31 | } 32 | 33 | func (v *serverVariable) Description() string { 34 | return v.description 35 | } 36 | 37 | func (v *serverVariable) Reference() string { 38 | return v.reference 39 | } 40 | 41 | func (v *serverVariable) IsUnresolved() bool { 42 | return v.reference != "" && !v.resolved 43 | } 44 | 45 | func (v *serverVariable) Validate(recurse bool) error { 46 | return Visit(context.Background(), newValidator(recurse), v) 47 | } 48 | -------------------------------------------------------------------------------- /v3/server_variable_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *ServerVariableMap) Clear() error { 16 | *v = make(ServerVariableMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in ServerVariableMap 21 | func (v *ServerVariableMap) Validate(recurse bool) error { 22 | for name, elem := range *v { 23 | if validator, ok := elem.(Validator); ok { 24 | if err := validator.Validate(recurse); err != nil { 25 | return errors.Wrapf(err, `failed to validate element %v`, name) 26 | } 27 | } 28 | } 29 | return nil 30 | } 31 | 32 | func (v ServerVariableMap) QueryJSON(path string) (ret interface{}, ok bool) { 33 | if path == `` { 34 | return v, true 35 | } 36 | 37 | var frag string 38 | frag, path = extractFragFromPath(path) 39 | target, ok := v[frag] 40 | if !ok { 41 | return nil, false 42 | } 43 | 44 | if qj, ok := target.(QueryJSONer); ok { 45 | return qj.QueryJSON(path) 46 | } 47 | 48 | if path == `` { 49 | return target, true 50 | } 51 | return nil, false 52 | } 53 | 54 | func (v *ServerVariableMap) UnmarshalJSON(data []byte) error { 55 | var proxy map[string]*serverVariable 56 | if err := json.Unmarshal(data, &proxy); err != nil { 57 | return errors.Wrap(err, `failed to unmarshal ServerVariableMap`) 58 | } 59 | tmp := make(map[string]ServerVariable) 60 | for name, value := range proxy { 61 | value.setName(name) 62 | tmp[name] = value 63 | } 64 | *v = tmp 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /v3/server_variable_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ServerVariableVisitor is an interface for objects that knows 16 | // how to process ServerVariable elements while traversing the OpenAPI structure 17 | type ServerVariableVisitor interface { 18 | VisitServerVariable(context.Context, ServerVariable) error 19 | } 20 | 21 | func visitServerVariable(ctx context.Context, elem ServerVariable) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(serverVariableVisitorCtxKey{}).(ServerVariableVisitor); ok { 29 | if err := v.VisitServerVariable(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit ServerVariable element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/server_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // ServerVisitor is an interface for objects that knows 16 | // how to process Server elements while traversing the OpenAPI structure 17 | type ServerVisitor interface { 18 | VisitServer(context.Context, Server) error 19 | } 20 | 21 | func visitServer(ctx context.Context, elem Server) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(serverVisitorCtxKey{}).(ServerVisitor); ok { 29 | if err := v.VisitServer(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Server element`) 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /v3/string_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *StringList) Clear() error { 16 | *v = StringList(nil) 17 | return nil 18 | } 19 | 20 | // Validate checks for the values for correctness. If `recurse` 21 | // is specified, child elements are also validated 22 | func (v *StringList) Validate(recurse bool) error { 23 | return nil 24 | } 25 | -------------------------------------------------------------------------------- /v3/string_list_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *StringListMap) Clear() error { 16 | *v = make(StringListMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in StringListMap 21 | func (v *StringListMap) Validate(recurse bool) error { 22 | return nil 23 | } 24 | 25 | func (v StringListMap) QueryJSON(path string) (ret interface{}, ok bool) { 26 | if path == `` { 27 | return v, true 28 | } 29 | 30 | var frag string 31 | frag, path = extractFragFromPath(path) 32 | target, ok := v[frag] 33 | if !ok { 34 | return nil, false 35 | } 36 | 37 | if path == `` { 38 | return target, true 39 | } 40 | return nil, false 41 | } 42 | -------------------------------------------------------------------------------- /v3/string_map_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *StringMap) Clear() error { 16 | *v = make(StringMap) 17 | return nil 18 | } 19 | 20 | // Validate checks the correctness of values in StringMap 21 | func (v *StringMap) Validate(recurse bool) error { 22 | return nil 23 | } 24 | 25 | func (v StringMap) QueryJSON(path string) (ret interface{}, ok bool) { 26 | if path == `` { 27 | return v, true 28 | } 29 | 30 | var frag string 31 | frag, path = extractFragFromPath(path) 32 | target, ok := v[frag] 33 | if !ok { 34 | return nil, false 35 | } 36 | 37 | if path == `` { 38 | return target, true 39 | } 40 | return nil, false 41 | } 42 | -------------------------------------------------------------------------------- /v3/tag_accessors_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = errors.Cause 13 | var _ = context.Background 14 | 15 | func (v *tag) Name() string { 16 | return v.name 17 | } 18 | 19 | func (v *tag) Description() string { 20 | return v.description 21 | } 22 | 23 | func (v *tag) ExternalDocs() ExternalDocumentation { 24 | return v.externalDocs 25 | } 26 | 27 | func (v *tag) Reference() string { 28 | return v.reference 29 | } 30 | 31 | func (v *tag) IsUnresolved() bool { 32 | return v.reference != "" && !v.resolved 33 | } 34 | 35 | func (v *tag) Validate(recurse bool) error { 36 | return Visit(context.Background(), newValidator(recurse), v) 37 | } 38 | -------------------------------------------------------------------------------- /v3/tag_list_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "encoding/json" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = json.Unmarshal 13 | var _ = errors.Cause 14 | 15 | func (v *TagList) Clear() error { 16 | *v = TagList(nil) 17 | return nil 18 | } 19 | 20 | // Validate checks for the values for correctness. If `recurse` 21 | // is specified, child elements are also validated 22 | func (v *TagList) Validate(recurse bool) error { 23 | for i, elem := range *v { 24 | if validator, ok := elem.(Validator); ok { 25 | if err := validator.Validate(recurse); err != nil { 26 | return errors.Wrapf(err, `failed to validate element %d`, i) 27 | } 28 | } 29 | } 30 | return nil 31 | } 32 | 33 | func (v *TagList) UnmarshalJSON(data []byte) error { 34 | var proxy []*tag 35 | if err := json.Unmarshal(data, &proxy); err != nil { 36 | return errors.Wrap(err, `failed to unmarshal TagList`) 37 | } 38 | 39 | if len(proxy) == 0 { 40 | *v = TagList(nil) 41 | return nil 42 | } 43 | 44 | tmp := make(TagList, len(proxy)) 45 | for i, value := range proxy { 46 | tmp[i] = value 47 | } 48 | *v = tmp 49 | return nil 50 | } 51 | -------------------------------------------------------------------------------- /v3/tag_mutator_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import "log" 7 | 8 | var _ = log.Printf 9 | 10 | // TagMutator is used to build an instance of Tag. The user must 11 | // call `Do()` after providing all the necessary information to 12 | // the new instance of Tag with new values 13 | type TagMutator struct { 14 | proxy *tag 15 | target *tag 16 | } 17 | 18 | // Do finalizes the matuation process for Tag and returns the result 19 | func (b *TagMutator) Do() error { 20 | *b.target = *b.proxy 21 | return nil 22 | } 23 | 24 | // MutateTag creates a new mutator object for Tag 25 | func MutateTag(v Tag) *TagMutator { 26 | return &TagMutator{ 27 | target: v.(*tag), 28 | proxy: v.Clone().(*tag), 29 | } 30 | } 31 | 32 | // Name sets the Name field for object Tag. 33 | func (b *TagMutator) Name(v string) *TagMutator { 34 | b.proxy.name = v 35 | return b 36 | } 37 | 38 | // Description sets the Description field for object Tag. 39 | func (b *TagMutator) Description(v string) *TagMutator { 40 | b.proxy.description = v 41 | return b 42 | } 43 | 44 | // ExternalDocs sets the ExternalDocs field for object Tag. 45 | func (b *TagMutator) ExternalDocs(v ExternalDocumentation) *TagMutator { 46 | b.proxy.externalDocs = v 47 | return b 48 | } 49 | -------------------------------------------------------------------------------- /v3/tag_visitor_gen.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | // This file was automatically generated. 4 | // DO NOT EDIT MANUALLY. All changes will be lost 5 | 6 | import ( 7 | "context" 8 | 9 | "github.com/pkg/errors" 10 | ) 11 | 12 | var _ = context.Background 13 | var _ = errors.Cause 14 | 15 | // TagVisitor is an interface for objects that knows 16 | // how to process Tag elements while traversing the OpenAPI structure 17 | type TagVisitor interface { 18 | VisitTag(context.Context, Tag) error 19 | } 20 | 21 | func visitTag(ctx context.Context, elem Tag) error { 22 | select { 23 | case <-ctx.Done(): 24 | return ctx.Err() 25 | default: 26 | } 27 | 28 | if v, ok := ctx.Value(tagVisitorCtxKey{}).(TagVisitor); ok { 29 | if err := v.VisitTag(ctx, elem); err != nil { 30 | if err == ErrVisitAbort { 31 | return nil 32 | } 33 | return errors.Wrap(err, `failed to visit Tag element`) 34 | } 35 | } 36 | 37 | if child := elem.ExternalDocs(); child != nil { 38 | if err := visitExternalDocumentation(ctx, child); err != nil { 39 | return errors.Wrap(err, `failed to visit ExternalDocs element for Tag`) 40 | } 41 | } 42 | return nil 43 | } 44 | -------------------------------------------------------------------------------- /v3/validator.go: -------------------------------------------------------------------------------- 1 | package openapi 2 | 3 | import ( 4 | "context" 5 | "strconv" 6 | "strings" 7 | 8 | "github.com/pkg/errors" 9 | ) 10 | 11 | type validator struct { 12 | recurse bool 13 | } 14 | 15 | func newValidator(recurse bool) *validator { 16 | return &validator{recurse: recurse} 17 | } 18 | 19 | func looksLikeSemanticVersion(s string) error { 20 | fields := strings.FieldsFunc(s, func(r rune) bool { 21 | return r == '.' 22 | }) 23 | if len(fields) != 3 { 24 | return errors.Errorf(`invalid number of fields in semantic version (%d)`, len(fields)) 25 | } 26 | if fields[0] != "3" { 27 | return errors.Errorf(`invalid major version in semantic version (%s)`, fields[0]) 28 | } 29 | 30 | for i := 1; i < 3; i++ { 31 | if _, err := strconv.Atoi(fields[i]); err != nil { 32 | return errors.Wrapf(err, `invalid semantic version component %s`, fields[i]) 33 | } 34 | } 35 | return nil 36 | } 37 | 38 | func (val *validator) VisitOpenAPI(ctx context.Context, v OpenAPI) error { 39 | if err := looksLikeSemanticVersion(v.Version()); err != nil { 40 | return errors.Wrapf(err, `openapi field must be a semantic version 3.x.x (got %s)`, strconv.Quote(v.Version())) 41 | } 42 | 43 | if v.Info() == nil { 44 | return errors.New(`info is required`) 45 | } 46 | 47 | if v.Paths() == nil { 48 | return errors.New(`paths is required`) 49 | } 50 | 51 | return nil 52 | } 53 | 54 | func (val *validator) VisitOperation(ctx context.Context, v Operation) error { 55 | if v.Responses() == nil { 56 | return errors.New(`missing required field "responses"`) 57 | } 58 | 59 | return nil 60 | } 61 | --------------------------------------------------------------------------------