Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/docs/openapi2conv.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func FromV3Responses(responses map[string]*openapi3.ResponseRef, components *ope
func FromV3SchemaRef(schema *openapi3.SchemaRef, components *openapi3.Components) (*openapi2.SchemaRef, *openapi2.Parameter)
func FromV3Schemas(schemas map[string]*openapi3.SchemaRef, components *openapi3.Components) (map[string]*openapi2.SchemaRef, map[string]*openapi2.Parameter)
func FromV3SecurityRequirements(requirements openapi3.SecurityRequirements) openapi2.SecurityRequirements
func FromV3SecurityScheme(doc3 *openapi3.T, ref *openapi3.SecuritySchemeRef) (*openapi2.SecurityScheme, error)
func FromV3SecurityScheme(ref *openapi3.SecuritySchemeRef) (*openapi2.SecurityScheme, error)
func ToV3(doc2 *openapi2.T) (*openapi3.T, error)
ToV3 converts an OpenAPIv2 spec to an OpenAPIv3 spec

func ToV3Headers(defs map[string]*openapi2.Header) openapi3.Headers
func ToV3Operation(doc2 *openapi2.T, components *openapi3.Components, pathItem *openapi2.PathItem, operation *openapi2.Operation, consumes []string) (*openapi3.Operation, error)
func ToV3Operation(components *openapi3.Components, pathItem *openapi2.PathItem, operation *openapi2.Operation, consumes []string) (*openapi3.Operation, error)
func ToV3Parameter(components *openapi3.Components, parameter *openapi2.Parameter, consumes []string) (*openapi3.ParameterRef, *openapi3.RequestBodyRef, map[string]*openapi3.SchemaRef, error)
func ToV3PathItem(doc2 *openapi2.T, components *openapi3.Components, pathItem *openapi2.PathItem, consumes []string) (*openapi3.PathItem, error)
func ToV3Ref(ref string) string
Expand Down
43 changes: 10 additions & 33 deletions .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ var IncludeOrigin = false

FUNCTIONS

func BoolPtr(value bool) *bool
BoolPtr is a helper for defining OpenAPI schemas.

Deprecated: Use Ptr instead.

func DefaultRefNameResolver(doc *T, ref ComponentRef) string
DefaultRefResolver is a default implementation of refNameResolver for the
InternalizeRefs function.
Expand All @@ -138,7 +133,7 @@ func DefaultRefNameResolver(doc *T, ref ComponentRef) string
- Cutting the "#/components/<type>" part.
- Cutting the file extensions (.yaml/.json) from documents.
- Trimming the common directory with the root spec.
- Replace invalid characters with with underscores.
- Replace invalid characters with underscores.

This is an injective mapping over a "reasonable" amount of the possible
openapi spec domain space but is not perfect. There might be edge cases.
Expand Down Expand Up @@ -174,19 +169,6 @@ func DefineStringFormatValidator(name string, validator StringFormatValidator)
DefineStringFormatValidator defines a custom format validator for a given
string format.

func Float64Ptr(value float64) *float64
Float64Ptr is a helper for defining OpenAPI schemas.

Deprecated: Use Ptr instead.

func Int64Ptr(value int64) *int64
Int64Ptr is a helper for defining OpenAPI schemas.

Deprecated: Use Ptr instead.

func Ptr[T any](value T) *T
Ptr is a helper for defining OpenAPI schemas.

func ReadFromFile(loader *Loader, location *url.URL) ([]byte, error)
ReadFromFile is a ReadFromURIFunc which reads local file URIs.

Expand Down Expand Up @@ -236,11 +218,6 @@ func RegisterArrayUniqueItemsChecker(fn SliceUniqueItemsChecker)
RegisterArrayUniqueItemsChecker is used to register a customized function
used to check if JSON array have unique items.

func Uint64Ptr(value uint64) *uint64
Uint64Ptr is a helper for defining OpenAPI schemas.

Deprecated: Use Ptr instead.

func ValidateIdentifier(value string) error
ValidateIdentifier returns an error if the given component name does not
match IdentifierRegExp.
Expand Down Expand Up @@ -364,7 +341,7 @@ func (x *CallbackRef) Validate(ctx context.Context, opts ...ValidationOption) er
Validate returns an error if CallbackRef does not comply with the OpenAPI
spec.

type Callbacks map[string]*CallbackRef
type Callbacks map[string]*CallbackRef // Callbacks represents components' named callbacks

func (m Callbacks) JSONLookup(token string) (any, error)
JSONLookup implements
Expand Down Expand Up @@ -591,7 +568,7 @@ func (x *ExampleRef) Validate(ctx context.Context, opts ...ValidationOption) err
Validate returns an error if ExampleRef does not comply with the OpenAPI
spec.

type Examples map[string]*ExampleRef
type Examples map[string]*ExampleRef // Examples represents components' named examples

func (m Examples) JSONLookup(token string) (any, error)
JSONLookup implements
Expand Down Expand Up @@ -731,7 +708,7 @@ func (x *HeaderRef) Validate(ctx context.Context, opts ...ValidationOption) erro
Validate returns an error if HeaderRef does not comply with the OpenAPI
spec.

type Headers map[string]*HeaderRef
type Headers map[string]*HeaderRef // Headers represents components' named headers

func (m Headers) JSONLookup(token string) (any, error)
JSONLookup implements
Expand Down Expand Up @@ -866,7 +843,7 @@ func (x *LinkRef) UnmarshalJSON(data []byte) error
func (x *LinkRef) Validate(ctx context.Context, opts ...ValidationOption) error
Validate returns an error if LinkRef does not comply with the OpenAPI spec.

type Links map[string]*LinkRef
type Links map[string]*LinkRef // Links represents components' named links

func (m Links) JSONLookup(token string) (any, error)
JSONLookup implements
Expand Down Expand Up @@ -1259,7 +1236,7 @@ func (parameters Parameters) Validate(ctx context.Context, opts ...ValidationOpt
Validate returns an error if Parameters does not comply with the OpenAPI
spec.

type ParametersMap map[string]*ParameterRef
type ParametersMap map[string]*ParameterRef // ParametersMap represents components' named parameters

func (m ParametersMap) JSONLookup(token string) (any, error)
JSONLookup implements
Expand Down Expand Up @@ -1432,7 +1409,7 @@ type RegexMatcher interface {
MatchString(s string) bool
}

type RequestBodies map[string]*RequestBodyRef
type RequestBodies map[string]*RequestBodyRef // RequestBodies represents components' named request bodies

func (m RequestBodies) JSONLookup(token string) (any, error)
JSONLookup implements
Expand Down Expand Up @@ -1562,7 +1539,7 @@ func (response *Response) WithJSONSchema(schema *Schema) *Response

func (response *Response) WithJSONSchemaRef(schema *SchemaRef) *Response

type ResponseBodies map[string]*ResponseRef
type ResponseBodies map[string]*ResponseRef // ResponseBodies represents components' named response bodies

func (m ResponseBodies) JSONLookup(token string) (any, error)
JSONLookup implements
Expand Down Expand Up @@ -2062,7 +2039,7 @@ func WithStringFormatValidators(validators map[string]StringFormatValidator) Sch
These validators are checked before global SchemaStringFormats and allow
different validations for the same format name across different specs.

type Schemas map[string]*SchemaRef
type Schemas map[string]*SchemaRef // Schemas represents components' named schemas

func (m Schemas) JSONLookup(token string) (any, error)
JSONLookup implements
Expand Down Expand Up @@ -2189,7 +2166,7 @@ func (x *SecuritySchemeRef) Validate(ctx context.Context, opts ...ValidationOpti
Validate returns an error if SecuritySchemeRef does not comply with the
OpenAPI spec.

type SecuritySchemes map[string]*SecuritySchemeRef
type SecuritySchemes map[string]*SecuritySchemeRef // SecuritySchemes represents components' named security schemes

func (m SecuritySchemes) JSONLookup(token string) (any, error)
JSONLookup implements
Expand Down
33 changes: 7 additions & 26 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,10 @@ jobs:
name: ${{ matrix.go }} on ${{ matrix.os }}
steps:

- uses: actions/setup-go@v3
- uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go }}

- id: go-cache-paths
run: |
echo "::set-output name=go-build::$(go env GOCACHE)"
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
- run: echo ${{ steps.go-cache-paths.outputs.go-build }}
- run: echo ${{ steps.go-cache-paths.outputs.go-mod }}

- name: Go Build Cache
uses: actions/cache@v3
with:
path: ${{ steps.go-cache-paths.outputs.go-build }}
key: ${{ runner.os }}-go-${{ matrix.go }}-build-${{ hashFiles('**/go.sum') }}

- name: Go Mod Cache (go>=1.15)
uses: actions/cache@v3
with:
path: ${{ steps.go-cache-paths.outputs.go-mod }}
key: ${{ runner.os }}-go-${{ matrix.go }}-mod-${{ hashFiles('**/go.sum') }}

- name: Test suite fixtures cache
uses: actions/cache@v3
with:
Expand All @@ -59,7 +40,7 @@ jobs:
- if: runner.os == 'Linux'
run: sudo apt install silversearcher-ag

- uses: actions/checkout@v2
- uses: actions/checkout@v6

- run: go generate ./...
- run: git --no-pager diff --exit-code
Expand Down Expand Up @@ -212,11 +193,11 @@ jobs:
check-goimports:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v3
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: '>=1.17.0'
- run: go install github.com/incu6us/goimports-reviser/v2@latest
go-version: '1.26'
- run: go install github.com/incu6us/goimports-reviser/v3@latest
- run: which goimports-reviser
- run: find . -type f -iname '*.go' ! -iname '*.pb.go' -exec goimports-reviser -file-path {} \;
- run: find . -type f -iname '*.go' ! -iname '*.pb.go' -exec goimports-reviser {} \;
- run: git --no-pager diff --exit-code
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Be sure to check [OpenAPI Initiative](https://github.com/OAI)'s [great tooling l
# Some recipes
## Validating an OpenAPI document
```shell
go run github.com/getkin/kin-openapi/cmd/validate@latest [--circular] [--defaults] [--examples] [--ext] [--patterns] -- <local YAML or JSON file>
go run github.com/getkin/kin-openapi/cmd/validate@latest [--defaults] [--examples] [--ext] [--patterns] -- <local YAML or JSON file>
```

## Loading OpenAPI document
Expand Down Expand Up @@ -317,6 +317,7 @@ for _, path := range doc.Paths.InMatchingOrder() {
* `openapi3.Schema.ExclusiveMin` and `openapi3.Schema.ExclusiveMax` fields changed from `bool` to `ExclusiveBound` (a union type holding `*bool` for OpenAPI 3.0 or `*float64` for OpenAPI 3.1).
* `openapi3.Schema.PrefixItems` field changed from `[]*SchemaRef` to `SchemaRefs`.
* `openapi3.Schema.UnevaluatedItems` and `openapi3.Schema.UnevaluatedProperties` fields changed from `*SchemaRef` to `BoolSchema` (a union type accepting either a boolean or a schema object).
* Removed `openapi3.*Ptr(..)` funcs: they all can be replaced with `new(..)` since Go 1.26

### v0.135.0
* `openapi3.MediaType.Encoding` field type changed from `map[string]*Encoding` to `Encodings`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/getkin/kin-openapi

go 1.26.2
go 1.26

require (
github.com/go-openapi/jsonpointer v0.21.0
Expand Down
5 changes: 2 additions & 3 deletions openapi2/openapi2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi2

import (
"encoding/json"
"maps"

"github.com/getkin/kin-openapi/openapi3"
)
Expand Down Expand Up @@ -30,9 +31,7 @@ type T struct {
// MarshalJSON returns the JSON encoding of T.
func (doc T) MarshalJSON() ([]byte, error) {
m := make(map[string]any, 15+len(doc.Extensions))
for k, v := range doc.Extensions {
m[k] = v
}
maps.Copy(m, doc.Extensions)
m["swagger"] = doc.Swagger
m["info"] = doc.Info
if x := doc.ExternalDocs; x != nil {
Expand Down
5 changes: 2 additions & 3 deletions openapi2/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi2

import (
"encoding/json"
"maps"

"github.com/getkin/kin-openapi/openapi3"
)
Expand All @@ -26,9 +27,7 @@ type Operation struct {
// MarshalJSON returns the JSON encoding of Operation.
func (operation Operation) MarshalJSON() ([]byte, error) {
m := make(map[string]any, 12+len(operation.Extensions))
for k, v := range operation.Extensions {
m[k] = v
}
maps.Copy(m, operation.Extensions)
if x := operation.Summary; x != "" {
m["summary"] = x
}
Expand Down
5 changes: 2 additions & 3 deletions openapi2/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi2

import (
"encoding/json"
"maps"

"github.com/getkin/kin-openapi/openapi3"
)
Expand Down Expand Up @@ -45,9 +46,7 @@ func (parameter Parameter) MarshalJSON() ([]byte, error) {
}

m := make(map[string]any, 24+len(parameter.Extensions))
for k, v := range parameter.Extensions {
m[k] = v
}
maps.Copy(m, parameter.Extensions)

if x := parameter.In; x != "" {
m["in"] = x
Expand Down
5 changes: 2 additions & 3 deletions openapi2/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openapi2
import (
"encoding/json"
"fmt"
"maps"
"net/http"

"github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -30,9 +31,7 @@ func (pathItem PathItem) MarshalJSON() ([]byte, error) {
}

m := make(map[string]any, 8+len(pathItem.Extensions))
for k, v := range pathItem.Extensions {
m[k] = v
}
maps.Copy(m, pathItem.Extensions)
if x := pathItem.Delete; x != nil {
m["delete"] = x
}
Expand Down
12 changes: 0 additions & 12 deletions openapi2/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ type SchemaRef struct {

var _ jsonpointer.JSONPointable = (*SchemaRef)(nil)

func (x *SchemaRef) isEmpty() bool { return x == nil || x.Ref == "" && x.Value == nil }

// RefString returns the $ref value.
func (x *SchemaRef) RefString() string { return x.Ref }

Expand All @@ -37,16 +35,6 @@ func (x *SchemaRef) CollectionName() string { return "schemas" }
// RefPath returns the path of the $ref relative to the root document.
func (x *SchemaRef) RefPath() *url.URL { return copyURI(x.refPath) }

func (x *SchemaRef) setRefPath(u *url.URL) {
// Once the refPath is set don't override. References can be loaded
// multiple times not all with access to the correct path info.
if x.refPath != nil {
return
}

x.refPath = copyURI(u)
}

// MarshalYAML returns the YAML encoding of SchemaRef.
func (x SchemaRef) MarshalYAML() (any, error) {
if ref := x.Ref; ref != "" {
Expand Down
5 changes: 2 additions & 3 deletions openapi2/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi2

import (
"encoding/json"
"maps"

"github.com/getkin/kin-openapi/openapi3"
)
Expand All @@ -24,9 +25,7 @@ func (response Response) MarshalJSON() ([]byte, error) {
}

m := make(map[string]any, 4+len(response.Extensions))
for k, v := range response.Extensions {
m[k] = v
}
maps.Copy(m, response.Extensions)
if x := response.Description; x != "" {
m["description"] = x
}
Expand Down
5 changes: 2 additions & 3 deletions openapi2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi2

import (
"encoding/json"
"maps"
"strings"

"github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -75,9 +76,7 @@ func (schema Schema) MarshalJSON() ([]byte, error) {
// MarshalYAML returns the YAML encoding of Schema.
func (schema Schema) MarshalYAML() (any, error) {
m := make(map[string]any, 36+len(schema.Extensions))
for k, v := range schema.Extensions {
m[k] = v
}
maps.Copy(m, schema.Extensions)

if x := schema.AllOf; len(x) != 0 {
m["allOf"] = x
Expand Down
5 changes: 2 additions & 3 deletions openapi2/security_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi2

import (
"encoding/json"
"maps"

"github.com/getkin/kin-openapi/openapi3"
)
Expand Down Expand Up @@ -31,9 +32,7 @@ func (securityScheme SecurityScheme) MarshalJSON() ([]byte, error) {
}

m := make(map[string]any, 10+len(securityScheme.Extensions))
for k, v := range securityScheme.Extensions {
m[k] = v
}
maps.Copy(m, securityScheme.Extensions)
if x := securityScheme.Description; x != "" {
m["description"] = x
}
Expand Down
Loading
Loading