-
-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openapi3: add support for extensions on the few types left
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
- Loading branch information
Showing
42 changed files
with
849 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package openapi3 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
) | ||
|
||
// Contact is specified by OpenAPI/Swagger standard version 3. | ||
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#contact-object | ||
type Contact struct { | ||
Extensions map[string]interface{} `json:"-" yaml:"-"` | ||
|
||
Name string `json:"name,omitempty" yaml:"name,omitempty"` | ||
URL string `json:"url,omitempty" yaml:"url,omitempty"` | ||
Email string `json:"email,omitempty" yaml:"email,omitempty"` | ||
} | ||
|
||
// MarshalJSON returns the JSON encoding of Contact. | ||
func (contact Contact) MarshalJSON() ([]byte, error) { | ||
m := make(map[string]interface{}, 3+len(contact.Extensions)) | ||
for k, v := range contact.Extensions { | ||
m[k] = v | ||
} | ||
if x := contact.Name; x != "" { | ||
m["name"] = x | ||
} | ||
if x := contact.URL; x != "" { | ||
m["url"] = x | ||
} | ||
if x := contact.Email; x != "" { | ||
m["email"] = x | ||
} | ||
return json.Marshal(m) | ||
} | ||
|
||
// UnmarshalJSON sets Contact to a copy of data. | ||
func (contact *Contact) UnmarshalJSON(data []byte) error { | ||
type ContactBis Contact | ||
var x ContactBis | ||
if err := json.Unmarshal(data, &x); err != nil { | ||
return err | ||
} | ||
_ = json.Unmarshal(data, &x.Extensions) | ||
delete(x.Extensions, "name") | ||
delete(x.Extensions, "url") | ||
delete(x.Extensions, "email") | ||
*contact = Contact(x) | ||
return nil | ||
} | ||
|
||
// Validate returns an error if Contact does not comply with the OpenAPI spec. | ||
func (contact *Contact) Validate(ctx context.Context, opts ...ValidationOption) error { | ||
ctx = WithValidationOptions(ctx, opts...) | ||
|
||
return validateExtensions(ctx, contact.Extensions) | ||
} |
Oops, something went wrong.