From 56505dc96565e100cd40a741c42a9cae58dd4fff Mon Sep 17 00:00:00 2001 From: Reuven Harrison Date: Wed, 16 Oct 2024 14:23:59 +0300 Subject: [PATCH] openapi3: introduce StringMap type to enable unmarshalling of maps with Origin (#1018) --- .github/docs/openapi3.txt | 16 ++++++++++------ README.md | 3 +++ openapi3/discriminator.go | 4 ++-- openapi3/security_scheme.go | 8 ++++---- openapi3/stringmap.go | 4 ++++ 5 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 openapi3/stringmap.go diff --git a/.github/docs/openapi3.txt b/.github/docs/openapi3.txt index cfccb97aa..eddcbfbd9 100644 --- a/.github/docs/openapi3.txt +++ b/.github/docs/openapi3.txt @@ -400,8 +400,8 @@ func (content Content) Validate(ctx context.Context, opts ...ValidationOption) e type Discriminator struct { Extensions map[string]any `json:"-" yaml:"-"` - PropertyName string `json:"propertyName" yaml:"propertyName"` // required - Mapping map[string]string `json:"mapping,omitempty" yaml:"mapping,omitempty"` + PropertyName string `json:"propertyName" yaml:"propertyName"` // required + Mapping StringMap `json:"mapping,omitempty" yaml:"mapping,omitempty"` } Discriminator is specified by OpenAPI/Swagger standard version 3. See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#discriminator-object @@ -873,10 +873,10 @@ type NumberFormatValidator = FormatValidator[float64] type OAuthFlow struct { Extensions map[string]any `json:"-" yaml:"-"` - AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"` - TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"` - RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"` - Scopes map[string]string `json:"scopes" yaml:"scopes"` // required + AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"` + TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"` + RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"` + Scopes StringMap `json:"scopes" yaml:"scopes"` // required } OAuthFlow is specified by OpenAPI/Swagger standard version 3. See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#oauth-flow-object @@ -1983,6 +1983,10 @@ func NewRegexpFormatValidator(pattern string) StringFormatValidator NewRegexpFormatValidator creates a new FormatValidator that uses a regular expression to validate the value. +type StringMap map[string]string + StringMap is a map[string]string that ignores the origin in the underlying + json representation. + type T struct { Extensions map[string]any `json:"-" yaml:"-"` diff --git a/README.md b/README.md index 41335937a..d135ffe96 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,9 @@ for _, path := range doc.Paths.InMatchingOrder() { ## CHANGELOG: Sub-v1 breaking API changes +### v0.129.0 +* `openapi3.Discriminator.Mapping` and `openapi3.OAuthFlow.Scopes` fields went from a `map[string]string` to the new type `StringMap` + ### v0.127.0 * Downgraded `github.com/gorilla/mux` dep from `1.8.1` to `1.8.0`. diff --git a/openapi3/discriminator.go b/openapi3/discriminator.go index e8193bd90..16d244008 100644 --- a/openapi3/discriminator.go +++ b/openapi3/discriminator.go @@ -10,8 +10,8 @@ import ( type Discriminator struct { Extensions map[string]any `json:"-" yaml:"-"` - PropertyName string `json:"propertyName" yaml:"propertyName"` // required - Mapping map[string]string `json:"mapping,omitempty" yaml:"mapping,omitempty"` + PropertyName string `json:"propertyName" yaml:"propertyName"` // required + Mapping StringMap `json:"mapping,omitempty" yaml:"mapping,omitempty"` } // MarshalJSON returns the JSON encoding of Discriminator. diff --git a/openapi3/security_scheme.go b/openapi3/security_scheme.go index b5c94b618..b37a6eda2 100644 --- a/openapi3/security_scheme.go +++ b/openapi3/security_scheme.go @@ -317,10 +317,10 @@ func (flows *OAuthFlows) Validate(ctx context.Context, opts ...ValidationOption) type OAuthFlow struct { Extensions map[string]any `json:"-" yaml:"-"` - AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"` - TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"` - RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"` - Scopes map[string]string `json:"scopes" yaml:"scopes"` // required + AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"` + TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"` + RefreshURL string `json:"refreshUrl,omitempty" yaml:"refreshUrl,omitempty"` + Scopes StringMap `json:"scopes" yaml:"scopes"` // required } // MarshalJSON returns the JSON encoding of OAuthFlow. diff --git a/openapi3/stringmap.go b/openapi3/stringmap.go new file mode 100644 index 000000000..3819851c9 --- /dev/null +++ b/openapi3/stringmap.go @@ -0,0 +1,4 @@ +package openapi3 + +// StringMap is a map[string]string that ignores the origin in the underlying json representation. +type StringMap map[string]string