Skip to content

Commit f2c3c2d

Browse files
akoclaude
andcommitted
fix: address review feedback on JSON STRUCTURE PR
- Sort custom name map keys for deterministic DESCRIBE output - Check errors from json.Decoder.Token() and Decode() in snippet parser - Add comment explaining why JsonElement uses int32 (verified vs Studio Pro) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 20dc4e4 commit f2c3c2d

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

mdl/executor/cmd_jsonstructures.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package executor
66
import (
77
"fmt"
88
"io"
9+
"sort"
910
"strings"
1011
"unicode"
1112

@@ -98,15 +99,20 @@ func (e *Executor) describeJsonStructure(name ast.QualifiedName) error {
9899
// Detect custom name mappings by comparing ExposedName to auto-generated names
99100
customMappings := collectCustomNameMappings(js.Elements)
100101
if len(customMappings) > 0 {
102+
// Sort keys for deterministic DESCRIBE output
103+
keys := make([]string, 0, len(customMappings))
104+
for k := range customMappings {
105+
keys = append(keys, k)
106+
}
107+
sort.Strings(keys)
108+
101109
fmt.Fprintf(e.output, "\n CUSTOM NAME MAP (\n")
102-
i := 0
103-
for jsonKey, customName := range customMappings {
110+
for i, jsonKey := range keys {
104111
sep := ","
105-
if i == len(customMappings)-1 {
112+
if i == len(keys)-1 {
106113
sep = ""
107114
}
108-
fmt.Fprintf(e.output, " '%s' AS '%s'%s\n", jsonKey, customName, sep)
109-
i++
115+
fmt.Fprintf(e.output, " '%s' AS '%s'%s\n", jsonKey, customMappings[jsonKey], sep)
110116
}
111117
fmt.Fprintf(e.output, " )")
112118
}

sdk/mpr/writer_jsonstructure.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ func serializeJsonStructure(js *JsonStructure) ([]byte, error) {
103103
return bson.Marshal(doc)
104104
}
105105

106+
// serializeJsonElement serializes a single JsonElement to BSON.
107+
// Note: JsonStructures$JsonElement uses int32 for numeric properties (MinOccurs, MaxOccurs, etc.),
108+
// unlike most other Mendix document types which use int64. Verified against Studio Pro-generated BSON.
106109
func serializeJsonElement(elem *JsonElement) bson.D {
107110
children := bson.A{int32(2)}
108111
for _, child := range elem.Children {
@@ -245,16 +248,23 @@ func (b *snippetBuilder) buildElementFromRawObject(exposedName, path, rawJSON st
245248

246249
// Decode with key order preserved
247250
dec := json.NewDecoder(strings.NewReader(rawJSON))
248-
dec.Token() // opening {
251+
if _, err := dec.Token(); err != nil { // opening {
252+
return elem
253+
}
249254
for dec.More() {
250-
tok, _ := dec.Token()
255+
tok, err := dec.Token()
256+
if err != nil {
257+
break
258+
}
251259
key, ok := tok.(string)
252260
if !ok {
253261
continue
254262
}
255263
// Capture the raw value to pass down for nested objects/arrays
256264
var rawVal json.RawMessage
257-
dec.Decode(&rawVal)
265+
if err := dec.Decode(&rawVal); err != nil {
266+
break
267+
}
258268

259269
childName := childTracker.uniqueName(b.resolveExposedName(key))
260270
childPath := path + "|" + key

0 commit comments

Comments
 (0)