Skip to content

Commit ba90a3f

Browse files
author
Phillip Wittrock
authored
Merge branch 'master' into crd-schema-primitive-types
2 parents d1acc4c + 9fb728d commit ba90a3f

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

cmd/internal/codegen/parse/crd.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ func (b *APIs) parseMapValidation(t *types.Type, found sets.String, comments []s
266266

267267
var arrayTemplate = template.Must(template.New("array-template").Parse(
268268
`v1beta1.JSONSchemaProps{
269-
Type: "array",
269+
Type: "{{.Type}}",
270+
{{ if .Format -}}
271+
Format: "{{.Format}}",
272+
{{ end -}}
270273
{{ if .MaxItems -}}
271274
MaxItems: getInt({{ .MaxItems }}),
272275
{{ end -}}
@@ -276,9 +279,11 @@ var arrayTemplate = template.Must(template.New("array-template").Parse(
276279
{{ if .UniqueItems -}}
277280
UniqueItems: {{ .UniqueItems }},
278281
{{ end -}}
282+
{{ if .Items -}}
279283
Items: &v1beta1.JSONSchemaPropsOrArray{
280284
Schema: &{{.ItemsSchema}},
281285
},
286+
{{ end -}}
282287
}`))
283288

284289
type arrayTemplateArgs struct {
@@ -294,6 +299,13 @@ func (b *APIs) parseArrayValidation(t *types.Type, found sets.String, comments [
294299
Type: "array",
295300
Items: &v1beta1.JSONSchemaPropsOrArray{Schema: &items},
296301
}
302+
// To represent byte arrays in the generated code, the property of the OpenAPI definition
303+
// should have string as its type and byte as its format.
304+
if t.Name.Name == "[]byte" {
305+
props.Type = "string"
306+
props.Format = "byte"
307+
props.Items = nil
308+
}
297309
for _, l := range comments {
298310
getValidation(l, &props)
299311
}

test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ function test_crd_validation {
531531
Alias string \`json:"alias"\`\
532532
// +kubebuilder:validation:Enum=1,2,3\
533533
Rank int \`json:"rank"\`\
534+
Comment []byte \`json:"comment,omitempty"\`\
534535
' pkg/apis/got/v1beta1/house_types.go
535536

536537
kubebuilder generate

test/data/resource/expected/crd-expected.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ spec:
3232
bricks:
3333
format: int32
3434
type: integer
35+
comment:
36+
format: byte
37+
type: string
3538
knights:
3639
items:
3740
type: string

0 commit comments

Comments
 (0)