Skip to content

Commit

Permalink
fix: de-duplicate required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Oct 24, 2024
1 parent 8772aea commit 3974f10
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion internal/converter/googleapi/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package googleapi

import (
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/sudorandom/protoc-gen-connect-openapi/internal/converter/util"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -25,7 +26,7 @@ func SchemaWithPropertyAnnotations(schema *base.Schema, desc protoreflect.FieldD
case annotations.FieldBehavior_OPTIONAL:
schema.Description = "(OPTIONAL) " + schema.Description
case annotations.FieldBehavior_REQUIRED:
schema.ParentProxy.Schema().Required = append(schema.ParentProxy.Schema().Required, string(desc.Name()))
schema.ParentProxy.Schema().Required = util.AppendStringDedupe(schema.ParentProxy.Schema().Required, string(desc.Name()))
case annotations.FieldBehavior_OUTPUT_ONLY:
schema.Description = "(OUTPUT_ONLY) " + schema.Description
case annotations.FieldBehavior_INPUT_ONLY:
Expand Down
5 changes: 3 additions & 2 deletions internal/converter/protovalidate/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/bufbuild/protovalidate-go/resolver"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/utils"
"github.com/sudorandom/protoc-gen-connect-openapi/internal/converter/util"
"google.golang.org/protobuf/reflect/protoreflect"
"gopkg.in/yaml.v3"
)
Expand All @@ -32,7 +33,7 @@ func SchemaWithFieldAnnotations(schema *base.Schema, desc protoreflect.FieldDesc
if constraints.Required != nil && *constraints.Required {
parent := schema.ParentProxy.Schema()
if parent != nil {
parent.Required = append(parent.Required, desc.JSONName())
parent.Required = util.AppendStringDedupe(parent.Required, desc.JSONName())
}
}
updateSchemaWithFieldConstraints(schema, constraints, onlyScalar)
Expand All @@ -49,7 +50,7 @@ func PopulateParentProperties(parent *base.Schema, desc protoreflect.FieldDescri
return parent
}
if constraints.Required != nil && *constraints.Required {
parent.Required = append(parent.Required, desc.JSONName())
parent.Required = util.AppendStringDedupe(parent.Required, desc.JSONName())
}
return parent
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
},
"title": "A",
"required": [
"b",
"b"
],
"additionalProperties": false
Expand All @@ -92,7 +91,6 @@
},
"title": "B",
"required": [
"c",
"c"
],
"additionalProperties": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ components:
title: A
required:
- b
- b
additionalProperties: false
foo.A.B:
type: object
Expand All @@ -53,7 +52,6 @@ components:
title: B
required:
- c
- c
additionalProperties: false
foo.A.B.C:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2312,7 +2312,6 @@
},
"title": "LotsOfValidationRules",
"required": [
"requiredField",
"requiredField"
],
"additionalProperties": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,6 @@ components:
title: LotsOfValidationRules
required:
- requiredField
- requiredField
additionalProperties: false
protovalidate.LotsOfValidationRules.MapKeysEntry:
type: object
Expand Down
9 changes: 9 additions & 0 deletions internal/converter/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,12 @@ func MakeFieldName(opts options.Options, fd protoreflect.FieldDescriptor) string
}
return fd.JSONName()
}

func AppendStringDedupe(strs []string, str string) []string {
for _, s := range strs {
if str == s {
return strs
}
}
return append(strs, str)
}

0 comments on commit 3974f10

Please sign in to comment.