richardfogaca opened a new pull request, #37455:
URL: https://github.com/apache/superset/pull/37455

   ### SUMMARY
   
   This PR fixes a bug where editing a dataset's metric currency configuration 
would fail with "The schema of the submitted payload is invalid" error.
   
   #### Root Cause
   
   The `currency` column was migrated from `VARCHAR` to `JSON` type in a 
previous migration, but some legacy data remained stored as stringified JSON 
(e.g., `'{"symbol": "EUR", "symbolPosition": "suffix"}'`). When this string 
value reached the frontend's `CurrencyControl` component, the spread operator 
would split it character-by-character:
   
   ```javascript
   // When currency is a string like '{"symbol": "EUR"}'
   onChange({ ...currency, symbolPosition: newValue })
   
   // JavaScript spreads strings as character keys:
   // {..."abc"} produces {"0": "a", "1": "b", "2": "c"}
   
   // Result: malformed payload
   {
     "0": "{",
     "1": "\"",
     "2": "s",
     ...
     "symbol": "EUR",
     "symbolPosition": "suffix"
   }
   ```
   
   This malformed object failed Marshmallow schema validation on the backend.
   
   #### Solution
   
   The fix implements defense-in-depth at three layers:
   
   1. **SQLAlchemy TypeDecorator** (`CurrencyType`) - Normalizes legacy string 
data when reading from the database, ensuring the API always returns a proper 
dict object.
   
   2. **Marshmallow Custom Field** (`CurrencyField`) - Handles edge cases where 
string payloads still reach the API, parsing them before validation.
   
   3. **Frontend Normalization** - The `CurrencyControl` component now 
normalizes its `value` prop, handling cases where a string is passed instead of 
an object.
   
   The `parse_currency_string` function handles multiple legacy formats:
   - Standard JSON strings: `'{"symbol": "USD"}'`
   - Python dict strings (single quotes): `"{'symbol': 'EUR'}"`
   - Double-encoded JSON: `'"{\\"symbol\\": \\"USD\\"}"'`
   
   ### TESTING INSTRUCTIONS
   
   1. Navigate to Datasets list
   2. Click edit on a dataset that has a metric with currency configured (e.g., 
"FCC 2018 Survey")
   3. Go to the Metrics tab and expand a metric row
   4. Verify the currency configuration displays correctly (e.g., "Suffix" and 
"€ (EUR)")
   5. Change the currency position from Suffix to Prefix
   6. Click Save and confirm
   7. Verify the save succeeds without errors
   8. Re-open the dataset and verify the change persisted
   
   **To simulate legacy data conditions:**
   ```sql
   -- Create a metric with stringified currency (simulates legacy data)
   UPDATE sql_metrics
   SET currency = '{"symbol": "EUR", "symbolPosition": "suffix"}'::jsonb
   WHERE id = <metric_id>;
   ```
   
   ### ADDITIONAL INFORMATION
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [x] Changes UI
   - [ ] Includes DB Migration (follow approval process in 
[SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] Migration is atomic, supports rollback & is backwards-compatible
     - [ ] Confirm DB migration upgrade and downgrade tested
     - [ ] Runtime estimates and downtime expectations provided
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to