Skip to content

Commit

Permalink
feat(protoc-gen-openapiv2/template): updateSwaggerObjectFromFieldBeha…
Browse files Browse the repository at this point in the history
…vior add default required rule
  • Loading branch information
Ccheers committed Feb 26, 2025
1 parent 57995ec commit 1b6fb42
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3156,23 +3156,31 @@ func updateswaggerObjectFromJSONSchema(s *openapiSchemaObject, j *openapi_option
}

func updateSwaggerObjectFromFieldBehavior(s *openapiSchemaObject, j []annotations.FieldBehavior, reg *descriptor.Registry, field *descriptor.Field) {
required := true
if field.GetProto3Optional() {
required = false
}
for _, fb := range j {
switch fb {
case annotations.FieldBehavior_REQUIRED:
if reg.GetUseJSONNamesForFields() {
s.Required = append(s.Required, *field.JsonName)
} else {
s.Required = append(s.Required, *field.Name)
}
required = true
case annotations.FieldBehavior_OUTPUT_ONLY:
s.ReadOnly = true
case annotations.FieldBehavior_FIELD_BEHAVIOR_UNSPECIFIED:
case annotations.FieldBehavior_OPTIONAL:
required = false
case annotations.FieldBehavior_INPUT_ONLY:
// OpenAPI v3 supports a writeOnly property, but this is not supported in Open API v2
case annotations.FieldBehavior_IMMUTABLE:
}
}
if required {
if reg.GetUseJSONNamesForFields() {
s.Required = append(s.Required, *field.JsonName)
} else {
s.Required = append(s.Required, *field.Name)
}
}
}

func openapiSchemaFromProtoEnumSchema(s *openapi_options.EnumSchema, reg *descriptor.Registry, refs refMap, data interface{}) openapiSchemaObject {
Expand Down

0 comments on commit 1b6fb42

Please sign in to comment.