Skip to content

Commit

Permalink
yaml tags
Browse files Browse the repository at this point in the history
  • Loading branch information
hedhyw committed Mar 13, 2022
1 parent d52eb18 commit cb19249
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 127 deletions.
16 changes: 1 addition & 15 deletions protoc-gen-openapiv2/internal/genopenapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,13 @@ func extensionMarshalJSON(so interface{}, extensions []extension) ([]byte, error

// encodeOpenAPI converts OpenAPI file obj to pluginpb.CodeGeneratorResponse_File
func encodeOpenAPI(file *wrapper, format Format) (*descriptor.ResponseFile, error) {
// file.swagger depends on JSON pretty much, because of tags and
// extensions, so do the trick, always encode and decode JSON and
// only then use selected encoder.

var contentBuf bytes.Buffer
if err := json.NewEncoder(&contentBuf).Encode(*file.swagger); err != nil {
return nil, err
}

var data map[string]interface{}
if err := json.NewDecoder(&contentBuf).Decode(&data); err != nil {
return nil, err
}

contentBuf.Reset()
enc, err := format.NewEncoder(&contentBuf)
if err != nil {
return nil, err
}

if err := enc.Encode(data); err != nil {
if err := enc.Encode(*file.swagger); err != nil {
return nil, err
}

Expand Down
1 change: 1 addition & 0 deletions protoc-gen-openapiv2/internal/genopenapi/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi"

"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
Expand Down
224 changes: 112 additions & 112 deletions protoc-gen-openapiv2/internal/genopenapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,41 @@ type param struct {

// http://swagger.io/specification/#infoObject
type openapiInfoObject struct {
Title string `json:"title"`
Description string `json:"description,omitempty"`
TermsOfService string `json:"termsOfService,omitempty"`
Version string `json:"version"`
Title string `json:"title" yaml:"title"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
TermsOfService string `json:"termsOfService,omitempty" yaml:"termsOfService,omitempty"`
Version string `json:"version" yaml:"version"`

Contact *openapiContactObject `json:"contact,omitempty"`
License *openapiLicenseObject `json:"license,omitempty"`
Contact *openapiContactObject `json:"contact,omitempty" yaml:"contact,omitempty"`
License *openapiLicenseObject `json:"license,omitempty" yaml:"license,omitempty"`

extensions []extension
extensions []extension `json:"-" yaml:"-"`
}

// https://swagger.io/specification/#tagObject
type openapiTagObject struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty"`
Name string `json:"name" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
}

// http://swagger.io/specification/#contactObject
type openapiContactObject struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Email string `json:"email,omitempty" yaml:"email,omitempty"`
}

// http://swagger.io/specification/#licenseObject
type openapiLicenseObject struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
}

// http://swagger.io/specification/#externalDocumentationObject
type openapiExternalDocumentationObject struct {
Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
}

type extension struct {
Expand All @@ -59,36 +59,36 @@ type extension struct {

// http://swagger.io/specification/#swaggerObject
type openapiSwaggerObject struct {
Swagger string `json:"swagger"`
Info openapiInfoObject `json:"info"`
Tags []openapiTagObject `json:"tags,omitempty"`
Host string `json:"host,omitempty"`
BasePath string `json:"basePath,omitempty"`
Schemes []string `json:"schemes,omitempty"`
Consumes []string `json:"consumes"`
Produces []string `json:"produces"`
Paths openapiPathsObject `json:"paths"`
Definitions openapiDefinitionsObject `json:"definitions"`
SecurityDefinitions openapiSecurityDefinitionsObject `json:"securityDefinitions,omitempty"`
Security []openapiSecurityRequirementObject `json:"security,omitempty"`
ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty"`

extensions []extension
Swagger string `json:"swagger" yaml:"swagger"`
Info openapiInfoObject `json:"info" yaml:"info"`
Tags []openapiTagObject `json:"tags,omitempty" yaml:"tags,omitempty"`
Host string `json:"host,omitempty" yaml:"host,omitempty"`
BasePath string `json:"basePath,omitempty" yaml:"basePath,omitempty"`
Schemes []string `json:"schemes,omitempty" yaml:"schemes,omitempty"`
Consumes []string `json:"consumes" yaml:"consumes"`
Produces []string `json:"produces" yaml:"produces"`
Paths openapiPathsObject `json:"paths" yaml:"paths"`
Definitions openapiDefinitionsObject `json:"definitions" yaml:"definitions"`
SecurityDefinitions openapiSecurityDefinitionsObject `json:"securityDefinitions,omitempty" yaml:"securityDefinitions,omitempty"`
Security []openapiSecurityRequirementObject `json:"security,omitempty" yaml:"security,omitempty"`
ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`

extensions []extension `json:"-" yaml:"-"`
}

// http://swagger.io/specification/#securityDefinitionsObject
type openapiSecurityDefinitionsObject map[string]openapiSecuritySchemeObject

// http://swagger.io/specification/#securitySchemeObject
type openapiSecuritySchemeObject struct {
Type string `json:"type"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
In string `json:"in,omitempty"`
Flow string `json:"flow,omitempty"`
AuthorizationURL string `json:"authorizationUrl,omitempty"`
TokenURL string `json:"tokenUrl,omitempty"`
Scopes openapiScopesObject `json:"scopes,omitempty"`
Type string `json:"type" yaml:"type"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
In string `json:"in,omitempty" yaml:"in,omitempty"`
Flow string `json:"flow,omitempty" yaml:"flow,omitempty"`
AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"`
TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"`
Scopes openapiScopesObject `json:"scopes,omitempty" yaml:"scopes,omitempty"`

extensions []extension
}
Expand All @@ -104,50 +104,50 @@ type openapiPathsObject map[string]openapiPathItemObject

// http://swagger.io/specification/#pathItemObject
type openapiPathItemObject struct {
Get *openapiOperationObject `json:"get,omitempty"`
Delete *openapiOperationObject `json:"delete,omitempty"`
Post *openapiOperationObject `json:"post,omitempty"`
Put *openapiOperationObject `json:"put,omitempty"`
Patch *openapiOperationObject `json:"patch,omitempty"`
Get *openapiOperationObject `json:"get,omitempty" yaml:"get,omitempty"`
Delete *openapiOperationObject `json:"delete,omitempty" yaml:"delete,omitempty"`
Post *openapiOperationObject `json:"post,omitempty" yaml:"post,omitempty"`
Put *openapiOperationObject `json:"put,omitempty" yaml:"put,omitempty"`
Patch *openapiOperationObject `json:"patch,omitempty" yaml:"patch,omitempty"`
}

// http://swagger.io/specification/#operationObject
type openapiOperationObject struct {
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
OperationID string `json:"operationId"`
Responses openapiResponsesObject `json:"responses"`
Parameters openapiParametersObject `json:"parameters,omitempty"`
Tags []string `json:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Produces []string `json:"produces,omitempty"`

Security *[]openapiSecurityRequirementObject `json:"security,omitempty"`
ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty"`

extensions []extension
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
OperationID string `json:"operationId" yaml:"operationId"`
Responses openapiResponsesObject `json:"responses" yaml:"responses"`
Parameters openapiParametersObject `json:"parameters,omitempty" yaml:"parameters,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
Produces []string `json:"produces,omitempty" yaml:"produces,omitempty"`

Security *[]openapiSecurityRequirementObject `json:"security,omitempty" yaml:"security,omitempty"`
ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`

extensions []extension `json:"-" yaml:"-"`
}

type openapiParametersObject []openapiParameterObject

// http://swagger.io/specification/#parameterObject
type openapiParameterObject struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
In string `json:"in,omitempty"`
Required bool `json:"required"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Items *openapiItemsObject `json:"items,omitempty"`
Enum []string `json:"enum,omitempty"`
CollectionFormat string `json:"collectionFormat,omitempty"`
Default string `json:"default,omitempty"`
MinItems *int `json:"minItems,omitempty"`
Pattern string `json:"pattern,omitempty"`
Name string `json:"name" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
In string `json:"in,omitempty" yaml:"in,omitempty"`
Required bool `json:"required" yaml:"required"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
Items *openapiItemsObject `json:"items,omitempty" yaml:"items,omitempty"`
Enum []string `json:"enum,omitempty" yaml:"enum,omitempty"`
CollectionFormat string `json:"collectionFormat,omitempty" yaml:"collectionFormat,omitempty"`
Default string `json:"default,omitempty" yaml:"default,omitempty"`
MinItems *int `json:"minItems,omitempty" yaml:"minItems,omitempty"`
Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"`

// Or you can explicitly refer to another type. If this is defined all
// other fields should be empty
Schema *openapiSchemaObject `json:"schema,omitempty"`
Schema *openapiSchemaObject `json:"schema,omitempty" yaml:"schema,omitempty"`
}

// core part of schema, which is common to itemsObject and schemaObject.
Expand All @@ -158,19 +158,19 @@ type openapiParameterObject struct {
// supported by generation tools such as swagger-codegen and go-swagger.
// For protoc-gen-openapiv3, we'd want to add `nullable` instead.
type schemaCore struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Ref string `json:"$ref,omitempty"`
XNullable bool `json:"x-nullable,omitempty"`
Example json.RawMessage `json:"example,omitempty"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
XNullable bool `json:"x-nullable,omitempty" yaml:"x-nullable,omitempty"`
Example json.RawMessage `json:"example,omitempty" yaml:"example,omitempty"`

Items *openapiItemsObject `json:"items,omitempty"`
Items *openapiItemsObject `json:"items,omitempty" yaml:"items,omitempty"`

// If the item is an enumeration include a list of all the *NAMES* of the
// enum values. I'm not sure how well this will work but assuming all enums
// start from 0 index it will be great. I don't think that is a good assumption.
Enum []string `json:"enum,omitempty"`
Default string `json:"default,omitempty"`
Enum []string `json:"enum,omitempty" yaml:"enum,omitempty"`
Default string `json:"default,omitempty" yaml:"default,omitempty"`
}

func (s *schemaCore) setRefFromFQN(ref string, reg *descriptor.Registry) error {
Expand All @@ -189,23 +189,23 @@ type openapiResponsesObject map[string]openapiResponseObject

// http://swagger.io/specification/#responseObject
type openapiResponseObject struct {
Description string `json:"description"`
Schema openapiSchemaObject `json:"schema"`
Examples map[string]interface{} `json:"examples,omitempty"`
Headers openapiHeadersObject `json:"headers,omitempty"`
Description string `json:"description" yaml:"description"`
Schema openapiSchemaObject `json:"schema" yaml:"schema"`
Examples map[string]interface{} `json:"examples,omitempty" yaml:"examples,omitempty"`
Headers openapiHeadersObject `json:"headers,omitempty" yaml:"headers,omitempty"`

extensions []extension
extensions []extension `json:"-" yaml:"-"`
}

type openapiHeadersObject map[string]openapiHeaderObject

// http://swagger.io/specification/#headerObject
type openapiHeaderObject struct {
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Default json.RawMessage `json:"default,omitempty"`
Pattern string `json:"pattern,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
Default json.RawMessage `json:"default,omitempty" yaml:"default,omitempty"`
Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"`
}

type keyVal struct {
Expand Down Expand Up @@ -243,29 +243,29 @@ func (op openapiSchemaObjectProperties) MarshalJSON() ([]byte, error) {
type openapiSchemaObject struct {
schemaCore
// Properties can be recursively defined
Properties *openapiSchemaObjectProperties `json:"properties,omitempty"`
AdditionalProperties *openapiSchemaObject `json:"additionalProperties,omitempty"`

Description string `json:"description,omitempty"`
Title string `json:"title,omitempty"`

ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty"`

ReadOnly bool `json:"readOnly,omitempty"`
MultipleOf float64 `json:"multipleOf,omitempty"`
Maximum float64 `json:"maximum,omitempty"`
ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
Minimum float64 `json:"minimum,omitempty"`
ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
MaxLength uint64 `json:"maxLength,omitempty"`
MinLength uint64 `json:"minLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
MaxItems uint64 `json:"maxItems,omitempty"`
MinItems uint64 `json:"minItems,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty"`
MaxProperties uint64 `json:"maxProperties,omitempty"`
MinProperties uint64 `json:"minProperties,omitempty"`
Required []string `json:"required,omitempty"`
Properties *openapiSchemaObjectProperties `json:"properties,omitempty" yaml:"properties,omitempty"`
AdditionalProperties *openapiSchemaObject `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`

Description string `json:"description,omitempty" yaml:"description,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`

ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`

ReadOnly bool `json:"readOnly,omitempty" yaml:"readOnly,omitempty"`
MultipleOf float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"`
Maximum float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"`
ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty" yaml:"exclusiveMaximum,omitempty"`
Minimum float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"`
ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty" yaml:"exclusiveMinimum,omitempty"`
MaxLength uint64 `json:"maxLength,omitempty" yaml:"maxLength,omitempty"`
MinLength uint64 `json:"minLength,omitempty" yaml:"minLength,omitempty"`
Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"`
MaxItems uint64 `json:"maxItems,omitempty" yaml:"maxItems,omitempty"`
MinItems uint64 `json:"minItems,omitempty" yaml:"minItems,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`
MaxProperties uint64 `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
MinProperties uint64 `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
}

// http://swagger.io/specification/#definitionsObject
Expand Down

0 comments on commit cb19249

Please sign in to comment.