Skip to content

Commit

Permalink
Fix unstructured map schema gen (#312)
Browse files Browse the repository at this point in the history
* Fix unstructured map schema gen

* codegen
  • Loading branch information
jahvon authored Nov 18, 2021
1 parent 94d0b0c commit 50914cb
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 41 deletions.
4 changes: 4 additions & 0 deletions changelog/v0.21.5/fix-unstructured-map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changelog:
- type: FIX
description: Fix CRD rendering for unstructured map fields
issueLink: https://github.com/solo-io/skv2/issues/311
14 changes: 11 additions & 3 deletions codegen/proto/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ func (o Options) getUnstructuredFields(protoPkg string, rootMessage []string) ([
}
var unstructuredFields [][]string
for _, field := range root.Fields {
fieldPath := []string{strcase.ToLowerCamel(field.Field.GetName())}
rawFieldPath := []string{strcase.ToLowerCamel(field.Field.GetName())}
if field.Field.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REPEATED {
// arrays become the path element '*' in the cue openapi builder
fieldPath = append(fieldPath, "*")
rawFieldPath = append(rawFieldPath, "*")
}

// Cue does not include "value" in it's path so we have to remove it from
// the fieldPath when it's included in map messages
var fieldPath []string
for _, fieldStr := range rawFieldPath {
if fieldStr != "value" {
fieldPath = append(fieldPath, fieldStr)
}
}

if field.OpenAPIValidationDisabled {
Expand All @@ -71,7 +80,6 @@ func (o Options) getUnstructuredFields(protoPkg string, rootMessage []string) ([
// the field is a primitive type
continue
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
// TODO: verify this works with map types
}

// check if this field has any unstructured fields in its children
Expand Down
168 changes: 131 additions & 37 deletions codegen/test/api/things.test.io/v1/test_api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion codegen/test/chart/crds/things.test.io_v1_crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
crd.solo.io/specHash: aa25e4ed1772db41
crd.solo.io/specHash: d651a92d5426c5e5
labels:
app: ""
app.kubernetes.io/name: ""
Expand Down Expand Up @@ -105,6 +105,17 @@ spec:
type: object
status:
properties:
nearbyPaints:
additionalProperties:
properties:
x:
type: object
x-kubernetes-preserve-unknown-fields: true
"y":
type: object
x-kubernetes-preserve-unknown-fields: true
type: object
type: object
observedGeneration:
format: int64
type: integer
Expand Down
6 changes: 6 additions & 0 deletions codegen/test/test_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ message OilType {
message PaintStatus {
int64 observedGeneration = 1;
int64 percentRemaining = 2;
map<string, Location> nearbyPaints = 3;

message Location {
string x = 1 [(cue.opt).disable_openapi_validation = true];
string y = 2 [(cue.opt).disable_openapi_validation = true];
}
}

message ClusterResourceSpec {
Expand Down

0 comments on commit 50914cb

Please sign in to comment.