Skip to content

Commit 5b37c1e

Browse files
Port 14830 bug bug in scab terraform (#261)
* add dynamic unknown fields to the blueprint property types * use reflaction to get the known fields * use any instead of interface * fix eyal pr comments
1 parent dcda3f4 commit 5b37c1e

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

internal/cli/models.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cli
22

33
import (
44
"encoding/json"
5+
"reflect"
6+
"strings"
57
"time"
68

79
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -489,6 +491,30 @@ type (
489491
}
490492
)
491493

494+
// getKnownFields uses reflection to extract JSON field names from BlueprintProperty struct
495+
func getKnownFields(bp *BlueprintProperty) map[string]bool {
496+
knownFields := make(map[string]bool)
497+
t := reflect.TypeOf(*bp)
498+
499+
for i := 0; i < t.NumField(); i++ {
500+
field := t.Field(i)
501+
502+
// Get the JSON tag
503+
jsonTag := field.Tag.Get("json")
504+
if jsonTag == "" || jsonTag == "-" {
505+
continue // Skip fields without JSON tags or with "-"
506+
}
507+
508+
// Handle "fieldname,omitempty" format
509+
fieldName, _, _ := strings.Cut(jsonTag, ",")
510+
if fieldName != "" {
511+
knownFields[fieldName] = true
512+
}
513+
}
514+
515+
return knownFields
516+
}
517+
492518
// Custom UnmarshalJSON for BlueprintProperty to capture dynamic fields
493519
func (bp *BlueprintProperty) UnmarshalJSON(data []byte) error {
494520
// Define an alias to avoid infinite recursion
@@ -506,21 +532,16 @@ func (bp *BlueprintProperty) UnmarshalJSON(data []byte) error {
506532
}
507533

508534
// Now unmarshal into a map to capture all fields
509-
var all map[string]interface{}
535+
var all map[string]any
510536
if err := json.Unmarshal(data, &all); err != nil {
511537
return err
512538
}
513539

514-
bp.UnknownFields = make(map[string]interface{})
540+
// Initialize UnknownFields map
541+
bp.UnknownFields = make(map[string]any)
515542

516-
// List of known fields that shouldn't go into UnknownFields
517-
knownFields := map[string]bool{
518-
"type": true, "title": true, "identifier": true, "items": true,
519-
"default": true, "icon": true, "format": true, "maxLength": true,
520-
"minLength": true, "maxItems": true, "minItems": true, "maximum": true,
521-
"minimum": true, "description": true, "blueprint": true, "pattern": true,
522-
"enum": true, "spec": true, "specAuthentication": true, "enumColors": true,
523-
}
543+
// Use reflection to get known fields instead of hardcoding
544+
knownFields := getKnownFields(bp)
524545

525546
// Add any unknown fields to UnknownFields
526547
for key, value := range all {
@@ -546,7 +567,7 @@ func (bp BlueprintProperty) MarshalJSON() ([]byte, error) {
546567
return nil, err
547568
}
548569

549-
var result map[string]interface{}
570+
var result map[string]any
550571
if err := json.Unmarshal(data, &result); err != nil {
551572
return nil, err
552573
}

0 commit comments

Comments
 (0)