Skip to content

Commit

Permalink
Add remaining openapi v3 typesO
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefftree committed Oct 21, 2021
1 parent 2fc535a commit 15a8e8b
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 25 deletions.
1 change: 0 additions & 1 deletion pkg/common/common.go
Expand Up @@ -62,7 +62,6 @@ type PathHandlerByGroupVersion interface {
HandlePrefix(path string, handler http.Handler)
}


// Config is set of configuration for openAPI spec generation.
type Config struct {
// List of supported protocols such as https, http, etc.
Expand Down
13 changes: 8 additions & 5 deletions pkg/spec3/component.go
Expand Up @@ -16,11 +16,14 @@ type Components struct {
Responses map[string]*Response `json:"responses,omitempty"`
// Parameters holds reusable Parameters Objects
Parameters map[string]*Parameter `json:"parameters,omitempty"`
// the following fields are missing:
// examples Map[string, Example Object | Reference Object]
// requestBodies Map[string, Request Body Object | Reference Object]
// headers Map[string, Header Object | Reference Object]
// links Map[string, Link Object | Reference Object]
// Example holds reusable Example objects
Examples map[string]*Example `json:"examples,omitempty"`
// RequestBodies holds reusable Request Body objects
RequestBodies map[string]*RequestBody `json:"requestBodies,omitempty"`
// Links is a map of operations links that can be followed from the response
Links map[string]*Link `json:"links,omitempty"`
// Headers holds a maps of a headers name to its definition
Headers map[string]*Header `json:"headers,omitempty"`
// callbacks Map[string, Callback Object | Reference Object]
//
// all fields are defined at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#componentsObject
Expand Down
44 changes: 44 additions & 0 deletions pkg/spec3/example.go
@@ -0,0 +1,44 @@
package spec3

import (
"encoding/json"

"github.com/go-openapi/spec"
"github.com/go-openapi/swag"
)

// Example https://swagger.io/specification/#example-object

type Example struct {
spec.Refable
ExampleProps
spec.VendorExtensible
}

// MarshalJSON is a custom marshal function that knows how to encode RequestBody as JSON
func (e *Example) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(e.Refable)
if err != nil {
return nil, err
}
b2, err := json.Marshal(e.ExampleProps)
if err != nil {
return nil, err
}
b3, err := json.Marshal(e.VendorExtensible)
if err != nil {
return nil, err
}
return swag.ConcatJSON(b1, b2, b3), nil
}

type ExampleProps struct {
// Summary holds a short description of the example
Summary string `json:"summary,omitempty"`
// Description holds a long description of the example
Description string `json:"description,omitempty"`
// Embedded literal example.
Value interface{} `json:"value,omitempty"`
// A URL that points to the literal example. This provides the capability to reference examples that cannot easily be included in JSON or YAML documents.
ExternalValue string `json:"externalValue,omitempty"`
}
73 changes: 73 additions & 0 deletions pkg/spec3/header.go
@@ -0,0 +1,73 @@
package spec3

import (
"encoding/json"

"github.com/go-openapi/swag"
"k8s.io/kube-openapi/pkg/validation/spec"
)

// Header a struct that describes a single operation parameter, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#parameterObject
//
// Note that this struct is actually a thin wrapper around HeaderProps to make it referable and extensible
type Header struct {
spec.Refable
HeaderProps
spec.VendorExtensible
}

// MarshalJSON is a custom marshal function that knows how to encode Header as JSON
func (h *Header) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(h.Refable)
if err != nil {
return nil, err
}
b2, err := json.Marshal(h.HeaderProps)
if err != nil {
return nil, err
}
b3, err := json.Marshal(h.VendorExtensible)
if err != nil {
return nil, err
}
return swag.ConcatJSON(b1, b2, b3), nil
}

func (h *Header) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &h.Refable); err != nil {
return err
}
if err := json.Unmarshal(data, &h.HeaderProps); err != nil {
return err
}
if err := json.Unmarshal(data, &h.VendorExtensible); err != nil {
return err
}

return nil
}

// HeaderProps a struct that describes a header object
type HeaderProps struct {
// Description holds a brief description of the parameter
Description string `json:"description,omitempty"`
// Required determines whether this parameter is mandatory
Required bool `json:"required,omitempty"`
// Deprecated declares this operation to be deprecated
Deprecated bool `json:"deprecated,omitempty"`
// AllowEmptyValue sets the ability to pass empty-valued parameters
AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`
// Style describes how the parameter value will be serialized depending on the type of the parameter value
Style string `json:"style,omitempty"`
// Explode when true, parameter values of type array or object generate separate parameters for each value of the array or key-value pair of the map
Explode bool `json:"explode,omitempty"`
// AllowReserved determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986
AllowReserved bool `json:"allowReserved,omitempty"`
// Schema holds the schema defining the type used for the parameter
Schema *spec.Schema `json:"schema,omitempty"`
// Content holds a map containing the representations for the parameter
Content map[string]*MediaType `json:"content,omitempty"`
// the following fields are missing:
// TODO: Example field is missing - (example Any Example of the media type. The example SHOULD match the specified schema and encoding properties if present. The example object is mutually exclusive of the examples object. Furthermore, if referencing a schema which contains an example, the example value SHALL override the example provided by the schema. To represent examples of media types that cannot naturally be represented in JSON or YAML, a string value can contain the example with escaping where necessary.)
// TODO: Examples field is missing - (examples Map[ string, Example Object | Reference Object] Examples of the media type. Each example SHOULD contain a value in the correct format as specified in the parameter encoding. The examples object is mutually exclusive of the example object. Furthermore, if referencing a schema which contains an example, the examples value SHALL override the example provided by the schema.)
}
53 changes: 48 additions & 5 deletions pkg/spec3/media_type.go
Expand Up @@ -8,7 +8,7 @@ import (

// MediaType a struct that allows you to specify content format, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#mediaTypeObject
//
// Note that this struct is actually a thin wrapper around MediaTypeProps to make it referable and extensibl
// Note that this struct is actually a thin wrapper around MediaTypeProps to make it referable and extensible
type MediaType struct {
MediaTypeProps
spec.VendorExtensible
Expand Down Expand Up @@ -41,8 +41,51 @@ func (m *MediaType) UnmarshalJSON(data []byte) error {
type MediaTypeProps struct {
// Schema holds the schema defining the type used for the media type
Schema *spec.Schema `json:"schema,omitempty"`
// the following fields are missing:
// TODO: Example field is missing - (example Any Example of the media type. The example object SHOULD be in the correct format as specified by the media type. The example object is mutually exclusive of the examples object. Furthermore, if referencing a schema which contains an example, the example value SHALL override the example provided by the schema.)
// TODO: Examples field is missing - (examples Map[ string, Example Object | Reference Object] Examples of the media type. Each example object SHOULD match the media type and specified schema if present. The examples object is mutually exclusive of the example object. Furthermore, if referencing a schema which contains an example, the examples value SHALL override the example provided by the schema.)
// TODO: Encoding field is missing - (encoding Map[string, Encoding Object] A map between a property name and its encoding information. The key, being the property name, MUST exist in the schema as a property. The encoding object SHALL only apply to requestBody objects when the media type is multipart or application/x-www-form-urlencoded.)
// Example of the media type
Example interface{} `json:"example,omitempty"`
// Examples of the media type. Each example object should match the media type and specific schema if present
Examples map[string]*Example `json:"examples,omitempty"`
// A map between a property name and its encoding information. The key, being the property name, MUST exist in the schema as a property. The encoding object SHALL only apply to requestBody objects when the media type is multipart or application/x-www-form-urlencoded
Encoding map[string]*Encoding `json:"encoding,omitempty"`
}

type Encoding struct {
EncodingProps
spec.VendorExtensible
}

// MarshalJSON is a custom marshal function that knows how to encode Encoding as JSON
func (e *Encoding) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(e.EncodingProps)
if err != nil {
return nil, err
}
b2, err := json.Marshal(e.VendorExtensible)
if err != nil {
return nil, err
}
return swag.ConcatJSON(b1, b2), nil
}

func (e *Encoding) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &e.EncodingProps); err != nil {
return err
}
if err := json.Unmarshal(data, &e.VendorExtensible); err != nil {
return err
}
return nil
}

type EncodingProps struct {
// Content Type for encoding a specific property
ContentType string `json:"contentType,omitempty"`
// A map allowing additional information to be provided as headers
Headers map[string]*Header `json:"headers,omitempty"`
// Describes how a specific property value will be serialized depending on its type
Style string `json:"style,omitempty"`
// When this is true, property values of type array or object generate separate parameters for each value of the array, or key-value-pair of the map. For other types of properties this property has no effect
Explode string `json:"explode,omitempty"`
// AllowReserved determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986
AllowReserved bool `json:"allowReserved,omitempty"`
}
21 changes: 18 additions & 3 deletions pkg/spec3/parameter.go
Expand Up @@ -33,6 +33,20 @@ func (p *Parameter) MarshalJSON() ([]byte, error) {
return swag.ConcatJSON(b1, b2, b3), nil
}

func (p *Parameter) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &p.Refable); err != nil {
return err
}
if err := json.Unmarshal(data, &p.ParameterProps); err != nil {
return err
}
if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
return err
}

return nil
}

// ParameterProps a struct that describes a single operation parameter, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#parameterObject
type ParameterProps struct {
// Name holds the name of the parameter
Expand All @@ -57,7 +71,8 @@ type ParameterProps struct {
Schema *spec.Schema `json:"schema,omitempty"`
// Content holds a map containing the representations for the parameter
Content map[string]*MediaType `json:"content,omitempty"`
// the following fields are missing:
// TODO: Example field is missing - (example Any Example of the media type. The example SHOULD match the specified schema and encoding properties if present. The example object is mutually exclusive of the examples object. Furthermore, if referencing a schema which contains an example, the example value SHALL override the example provided by the schema. To represent examples of media types that cannot naturally be represented in JSON or YAML, a string value can contain the example with escaping where necessary.)
// TODO: Examples field is missing - (examples Map[ string, Example Object | Reference Object] Examples of the media type. Each example SHOULD contain a value in the correct format as specified in the parameter encoding. The examples object is mutually exclusive of the example object. Furthermore, if referencing a schema which contains an example, the examples value SHALL override the example provided by the schema.)
// Example of the parameter's potential value
Example interface{} `json:"example,omitempty"`
// Examples of the parameter's potential value. Each example SHOULD contain a value in the correct format as specified in the parameter encoding
Examples map[string]*Example `json:"examples,omitempty"`
}
120 changes: 115 additions & 5 deletions pkg/spec3/path.go
Expand Up @@ -108,7 +108,8 @@ type PathProps struct {
Patch *Operation `json:"patch,omitempty"`
// Trace defines TRACE operation
Trace *Operation `json:"trace,omitempty"`
// TODO: Servers field is missing - (servers [Server Object] An alternative server array to service all operations in this path.)
// Servers is an alternative server array to service all operations in this path
Servers []*Server `json:"servers,omitempty"`
// Parameters a list of parameters that are applicable for this operation
Parameters []*Parameter `json:"parameters,omitempty"`
}
Expand Down Expand Up @@ -150,13 +151,14 @@ type OperationProps struct {
Summary string `json:"summary,omitempty"`
// Description holds a verbose explanation of the operation behavior
Description string `json:"description,omitempty"`
// TODO: ExternalDocs field is missing - (externalDocs External Documentation Object Additional external documentation for this operation)
// ExternalDocs holds additional external documentation for this operation
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
// OperationId holds a unique string used to identify the operation
OperationId string `json:"operationId,omitempty"`
// Parameters a list of parameters that are applicable for this operation
// Parameters []*Parameter `json:"parameters,omitempty"`
Parameters []*Parameter `json:"parameters,omitempty"`
// RequestBody holds the request body applicable for this operation
// RequestBody *RequestBody `json:"requestBody,omitempty"`
RequestBody *RequestBody `json:"requestBody,omitempty"`
// Responses holds the list of possible responses as they are returned from executing this operation
Responses *Responses `json:"responses,omitempty"`
// TODO: Callbacks field is missing - (callbacks Map[string, Callback Object | Reference Object] A map of possible out-of band callbacks related to the parent operation. The key is a unique identifier for the Callback Object. Each value in the map is a Callback Object that describes a request that may be initiated by the API provider and the expected responses. The key value used to identify the callback object is an expression, evaluated at runtime, that identifies a URL to use for the callback operation.
Expand Down Expand Up @@ -198,7 +200,6 @@ func (r *Responses) UnmarshalJSON(data []byte) error {
return nil
}


// ResponsesProps holds the list of possible responses as they are returned from executing this operation
type ResponsesProps struct {
// Default holds the documentation of responses other than the ones declared for specific HTTP response codes. Use this field to cover undeclared responses
Expand Down Expand Up @@ -239,3 +240,112 @@ func (r *ResponsesProps) UnmarshalJSON(data []byte) error {
}
return nil
}

type ExternalDocumentation struct {
ExternalDocumentationProps
spec.VendorExtensible
}

type ExternalDocumentationProps struct {
// Description is a short description of the target documentation. CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty"`
// URL is the URL for the target documentation.
URL string `json:"url"`
}

// MarshalJSON is a custom marshal function that knows how to encode Responses as JSON
func (e *ExternalDocumentation) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(e.ExternalDocumentationProps)
if err != nil {
return nil, err
}
b2, err := json.Marshal(e.VendorExtensible)
if err != nil {
return nil, err
}
return swag.ConcatJSON(b1, b2), nil
}

func (e *ExternalDocumentation) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &e.ExternalDocumentationProps); err != nil {
return err
}
if err := json.Unmarshal(data, &e.VendorExtensible); err != nil {
return err
}
return nil
}

type Server struct {
ServerProps
spec.VendorExtensible
}

type ServerProps struct {
// Description is a short description of the target documentation. CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty"`
// URL is the URL for the target documentation.
URL string `json:"url"`
// Variables contains a map between a variable name and its value. The value is used for substitution in the server's URL templeate
Variables map[string]*ServerVariable `json:"variables,omitempty"`
}

// MarshalJSON is a custom marshal function that knows how to encode Responses as JSON
func (s *Server) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(s.ServerProps)
if err != nil {
return nil, err
}
b2, err := json.Marshal(s.VendorExtensible)
if err != nil {
return nil, err
}
return swag.ConcatJSON(b1, b2), nil
}

func (s *Server) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &s.ServerProps); err != nil {
return err
}
if err := json.Unmarshal(data, &s.VendorExtensible); err != nil {
return err
}
return nil
}

type ServerVariable struct {
ServerVariableProps
spec.VendorExtensible
}

type ServerVariableProps struct {
// Enum is an enumeration of string values to be used if the substitution options are from a limited set
Enum []string `json:"enum,omitempty"`
// Default is the default value to use for substitution, which SHALL be sent if an alternate value is not supplied
Default string `json:"default"`
// Description is a description for the server variable
Description string `json:"description,omitempty"`
}

// MarshalJSON is a custom marshal function that knows how to encode Responses as JSON
func (s *ServerVariable) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(s.ServerVariableProps)
if err != nil {
return nil, err
}
b2, err := json.Marshal(s.VendorExtensible)
if err != nil {
return nil, err
}
return swag.ConcatJSON(b1, b2), nil
}

func (s *ServerVariable) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &s.ServerVariableProps); err != nil {
return err
}
if err := json.Unmarshal(data, &s.VendorExtensible); err != nil {
return err
}
return nil
}

0 comments on commit 15a8e8b

Please sign in to comment.