Skip to content

Commit

Permalink
openapi3: reclaims Extensions unused mem post-unmarshal (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp authored Nov 26, 2023
1 parent 4305869 commit bd3ac0e
Show file tree
Hide file tree
Showing 29 changed files with 98 additions and 3 deletions.
3 changes: 3 additions & 0 deletions openapi2/openapi2.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func (doc *T) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "securityDefinitions")
delete(x.Extensions, "security")
delete(x.Extensions, "tags")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*doc = T(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi2/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (operation *Operation) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "produces")
delete(x.Extensions, "schemes")
delete(x.Extensions, "security")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*operation = Operation(x)
return nil
}
4 changes: 4 additions & 0 deletions openapi2/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func (parameter *Parameter) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "minItems")
delete(x.Extensions, "default")

if len(x.Extensions) == 0 {
x.Extensions = nil
}

*parameter = Parameter(x)
return nil
}
3 changes: 3 additions & 0 deletions openapi2/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func (pathItem *PathItem) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "post")
delete(x.Extensions, "put")
delete(x.Extensions, "parameters")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*pathItem = PathItem(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi2/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (response *Response) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "schema")
delete(x.Extensions, "headers")
delete(x.Extensions, "examples")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*response = Response(x)
return nil
}
3 changes: 3 additions & 0 deletions openapi2/security_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (securityScheme *SecurityScheme) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "tokenUrl")
delete(x.Extensions, "scopes")
delete(x.Extensions, "tags")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*securityScheme = SecurityScheme(x)
return nil
}
3 changes: 3 additions & 0 deletions openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,9 @@ func FromV3SchemaRef(schema *openapi3.SchemaRef, components *openapi3.Components
}
if schema.Value.Nullable {
schema.Value.Nullable = false
if schema.Value.Extensions == nil {
schema.Value.Extensions = make(map[string]interface{})
}
schema.Value.Extensions["x-nullable"] = true
}

Expand Down
3 changes: 3 additions & 0 deletions openapi3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (components *Components) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "examples")
delete(x.Extensions, "links")
delete(x.Extensions, "callbacks")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*components = Components(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func (contact *Contact) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "name")
delete(x.Extensions, "url")
delete(x.Extensions, "email")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*contact = Contact(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/discriminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (discriminator *Discriminator) UnmarshalJSON(data []byte) error {
_ = json.Unmarshal(data, &x.Extensions)
delete(x.Extensions, "propertyName")
delete(x.Extensions, "mapping")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*discriminator = Discriminator(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (encoding *Encoding) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "style")
delete(x.Extensions, "explode")
delete(x.Extensions, "allowReserved")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*encoding = Encoding(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func (example *Example) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "description")
delete(x.Extensions, "value")
delete(x.Extensions, "externalValue")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*example = Example(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/external_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (e *ExternalDocs) UnmarshalJSON(data []byte) error {
_ = json.Unmarshal(data, &x.Extensions)
delete(x.Extensions, "description")
delete(x.Extensions, "url")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*e = ExternalDocs(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func (info *Info) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "contact")
delete(x.Extensions, "license")
delete(x.Extensions, "version")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*info = Info(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (license *License) UnmarshalJSON(data []byte) error {
_ = json.Unmarshal(data, &x.Extensions)
delete(x.Extensions, "name")
delete(x.Extensions, "url")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*license = License(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (link *Link) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "parameters")
delete(x.Extensions, "server")
delete(x.Extensions, "requestBody")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*link = Link(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/media_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func (mediaType *MediaType) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "example")
delete(x.Extensions, "examples")
delete(x.Extensions, "encoding")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*mediaType = MediaType(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func (doc *T) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "servers")
delete(x.Extensions, "tags")
delete(x.Extensions, "externalDocs")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*doc = T(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func (operation *Operation) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "security")
delete(x.Extensions, "servers")
delete(x.Extensions, "externalDocs")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*operation = Operation(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ func (parameter *Parameter) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "example")
delete(x.Extensions, "examples")
delete(x.Extensions, "content")
if len(x.Extensions) == 0 {
x.Extensions = nil
}

*parameter = Parameter(x)
return nil
Expand Down
3 changes: 3 additions & 0 deletions openapi3/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func (pathItem *PathItem) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "trace")
delete(x.Extensions, "servers")
delete(x.Extensions, "parameters")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*pathItem = PathItem(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func (requestBody *RequestBody) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "description")
delete(x.Extensions, "required")
delete(x.Extensions, "content")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*requestBody = RequestBody(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func (response *Response) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "headers")
delete(x.Extensions, "content")
delete(x.Extensions, "links")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*response = Response(x)
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ func (schema *Schema) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "additionalProperties")
delete(x.Extensions, "discriminator")

if len(x.Extensions) == 0 {
x.Extensions = nil
}

*schema = Schema(x)

if schema.Format == "date" {
Expand Down
9 changes: 9 additions & 0 deletions openapi3/security_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func (ss *SecurityScheme) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "bearerFormat")
delete(x.Extensions, "flows")
delete(x.Extensions, "openIdConnectUrl")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*ss = SecurityScheme(x)
return nil
}
Expand Down Expand Up @@ -272,6 +275,9 @@ func (flows *OAuthFlows) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "password")
delete(x.Extensions, "clientCredentials")
delete(x.Extensions, "authorizationCode")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*flows = OAuthFlows(x)
return nil
}
Expand Down Expand Up @@ -349,6 +355,9 @@ func (flow *OAuthFlow) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "tokenUrl")
delete(x.Extensions, "refreshUrl")
delete(x.Extensions, "scopes")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*flow = OAuthFlow(x)
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions openapi3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func (server *Server) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "url")
delete(x.Extensions, "description")
delete(x.Extensions, "variables")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*server = Server(x)
return nil
}
Expand Down Expand Up @@ -258,6 +261,9 @@ func (serverVariable *ServerVariable) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "enum")
delete(x.Extensions, "default")
delete(x.Extensions, "description")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*serverVariable = ServerVariable(x)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (t *Tag) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "name")
delete(x.Extensions, "description")
delete(x.Extensions, "externalDocs")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*t = Tag(x)
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions openapi3/validation_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ type validationOptionsKey struct{}
// AllowExtraSiblingFields called as AllowExtraSiblingFields("description") makes Validate not return an error when said field appears next to a $ref.
func AllowExtraSiblingFields(fields ...string) ValidationOption {
return func(options *ValidationOptions) {
if options.extraSiblingFieldsAllowed == nil && len(fields) != 0 {
options.extraSiblingFieldsAllowed = make(map[string]struct{}, len(fields))
}
for _, field := range fields {
if options.extraSiblingFieldsAllowed == nil {
options.extraSiblingFieldsAllowed = make(map[string]struct{}, len(fields))
}
options.extraSiblingFieldsAllowed[field] = struct{}{}
}
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (xml *XML) UnmarshalJSON(data []byte) error {
delete(x.Extensions, "prefix")
delete(x.Extensions, "attribute")
delete(x.Extensions, "wrapped")
if len(x.Extensions) == 0 {
x.Extensions = nil
}
*xml = XML(x)
return nil
}
Expand Down

0 comments on commit bd3ac0e

Please sign in to comment.