@@ -62,7 +62,6 @@ func TestBuildOpenAPISchema(t *testing.T) {
6262 },
6363 want: &extv1.JSONSchemaProps{
6464 Type: "object",
65- Default: &extv1.JSON{Raw: []byte("{}")},
6665 Required: []string{"name"},
6766 Properties: map[string]extv1.JSONSchemaProps{
6867 "name": {Type: "string"},
@@ -1117,6 +1116,68 @@ func TestBuildOpenAPISchema(t *testing.T) {
11171116 want: nil,
11181117 wantErr: true,
11191118 },
1119+ {
1120+ name: "Parent default set when no required fields and child has defaults",
1121+ obj: map[string]interface{}{
1122+ "timeout": "integer | default=30",
1123+ "retries": "integer | default=3",
1124+ },
1125+ want: &extv1.JSONSchemaProps{
1126+ Type: "object",
1127+ Default: &extv1.JSON{Raw: []byte("{}")},
1128+ Properties: map[string]extv1.JSONSchemaProps{
1129+ "timeout": {
1130+ Type: "integer",
1131+ Default: &extv1.JSON{Raw: []byte("30")},
1132+ },
1133+ "retries": {
1134+ Type: "integer",
1135+ Default: &extv1.JSON{Raw: []byte("3")},
1136+ },
1137+ },
1138+ },
1139+ wantErr: false,
1140+ },
1141+ {
1142+ name: "Parent default NOT set when has required fields even with child defaults",
1143+ obj: map[string]interface{}{
1144+ "name": "string | required=true",
1145+ "timeout": "integer | default=30",
1146+ },
1147+ want: &extv1.JSONSchemaProps{
1148+ Type: "object",
1149+ Required: []string{"name"},
1150+ Properties: map[string]extv1.JSONSchemaProps{
1151+ "name": {
1152+ Type: "string",
1153+ },
1154+ "timeout": {
1155+ Type: "integer",
1156+ Default: &extv1.JSON{Raw: []byte("30")},
1157+ },
1158+ },
1159+ },
1160+ wantErr: false,
1161+ },
1162+ {
1163+ name: "Parent default NOT set when no child defaults even without required fields",
1164+ obj: map[string]interface{}{
1165+ "name": "string",
1166+ "timeout": "integer",
1167+ },
1168+ want: &extv1.JSONSchemaProps{
1169+ Type: "object",
1170+ Properties: map[string]extv1.JSONSchemaProps{
1171+ "name": {
1172+ Type: "string",
1173+ },
1174+ "timeout": {
1175+ Type: "integer",
1176+ },
1177+ },
1178+ },
1179+ wantErr: false,
1180+ },
11201181 }
11211182
11221183 for _, tt := range tests {
0 commit comments