Skip to content

Commit 9fb728d

Browse files
author
Phillip Wittrock
authored
Merge pull request #244 from Liujingfang1/byte
fix crd validation spec for byte arrays
2 parents 6abddb1 + 0fddc34 commit 9fb728d

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
@@ -264,7 +264,10 @@ func (b *APIs) parseMapValidation(t *types.Type, found sets.String, comments []s
264264

265265
var arrayTemplate = template.Must(template.New("array-template").Parse(
266266
`v1beta1.JSONSchemaProps{
267-
Type: "array",
267+
Type: "{{.Type}}",
268+
{{ if .Format -}}
269+
Format: "{{.Format}}",
270+
{{ end -}}
268271
{{ if .MaxItems -}}
269272
MaxItems: getInt({{ .MaxItems }}),
270273
{{ end -}}
@@ -274,9 +277,11 @@ var arrayTemplate = template.Must(template.New("array-template").Parse(
274277
{{ if .UniqueItems -}}
275278
UniqueItems: {{ .UniqueItems }},
276279
{{ end -}}
280+
{{ if .Items -}}
277281
Items: &v1beta1.JSONSchemaPropsOrArray{
278282
Schema: &{{.ItemsSchema}},
279283
},
284+
{{ end -}}
280285
}`))
281286

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

test.sh

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

535536
kubebuilder generate

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ spec:
2929
- Wolf
3030
- Dragon
3131
type: string
32+
comment:
33+
format: byte
34+
type: string
3235
knights:
3336
items:
3437
type: string

0 commit comments

Comments
 (0)