Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use_allof_for_refs: extend to example field #3240

Merged
merged 1 commit into from Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Expand Up @@ -522,13 +522,16 @@ func renderMessageAsDefinition(msg *descriptor.Message, reg *descriptor.Registry
if fieldSchema.Ref != "" {
// Per the JSON Reference syntax: Any members other than "$ref" in a JSON Reference object SHALL be ignored.
// https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03#section-3
// However, use allOf to specify Title/Description/readOnly fields.
if fieldSchema.Title != "" || fieldSchema.Description != "" || fieldSchema.ReadOnly {
// However, use allOf to specify Title/Description/Example/readOnly fields.
if fieldSchema.Title != "" || fieldSchema.Description != "" || len(fieldSchema.Example) > 0 || fieldSchema.ReadOnly {
fieldSchema = openapiSchemaObject{
Title: fieldSchema.Title,
Description: fieldSchema.Description,
ReadOnly: fieldSchema.ReadOnly,
AllOf: []allOfEntry{{Ref: fieldSchema.Ref}},
schemaCore: schemaCore{
Example: fieldSchema.Example,
},
ReadOnly: fieldSchema.ReadOnly,
AllOf: []allOfEntry{{Ref: fieldSchema.Ref}},
}
} else {
fieldSchema = openapiSchemaObject{schemaCore: schemaCore{Ref: fieldSchema.Ref}}
Expand Down
17 changes: 17 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template_test.go
Expand Up @@ -5372,6 +5372,18 @@ func TestRenderMessagesAsDefinition(t *testing.T) {
},
},
},
openAPIOptions: &openapiconfig.OpenAPIOptions{
Field: []*openapiconfig.OpenAPIFieldOption{
{
Field: "example.Message.nested",
Option: &openapi_options.JSONSchema{
Title: "nested field title",
Description: "nested field desc",
Example: `"ok":"TRUE"`,
},
},
},
},
defs: map[string]openapiSchemaObject{
"exampleMessage": {
schemaCore: schemaCore{
Expand All @@ -5386,6 +5398,11 @@ func TestRenderMessagesAsDefinition(t *testing.T) {
Value: openapiSchemaObject{
AllOf: []allOfEntry{{Ref: "#/definitions/MessageNested"}},
ReadOnly: true,
schemaCore: schemaCore{
Example: RawExample(`"ok":"TRUE"`),
},
Title: "nested field title",
Description: "nested field desc",
},
},
},
Expand Down