Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed review comments on the typed preferences pull request

Damian Legawiec693628f49e78
diff --git a/docs/plans/6.0-typed-preferences.md b/docs/plans/6.0-typed-preferences.md
index c5476ab906..b7a36a25d2 100644
--- a/docs/plans/6.0-typed-preferences.md
+++ b/docs/plans/6.0-typed-preferences.md
@@ -14,7 +14,7 @@ Preferences reach the API in two ways, and both expose the Ruby preferences DSL
## Key Decisions (do not deviate without discussion)
- **The preference declarations remain the single source** (continuing `6.0-json-preferences.md`). The JSON Schema, the OpenAPI components, the SDK types, the `/…/types` responses and write validation are all generated from `preference :name, :type, …`. Nothing is maintained by hand.
-- **Declarations become complete.** `:array` requires `of:`. `:hash` requires `keys:` and `values:`. An ID list declares `of: :id, prefix:`, which replaces its `parse_on_set` decoding lambda. Money values declare `money: true`. Fixed vocabularies declare `choices:`. Domain formats (`:currency`, `:iso_country`, `:date`, `:timezone`) declare `format:`. A declaration the generator cannot compile fails a spec, not production.
+- **Declarations become complete.** `:array` requires `of:`. `:hash` requires `keys:` and `values:`. An ID list declares `of: :id, model:, scope:`, which replaces its `parse_on_set` decoding lambda. Money values declare `money: true`. Fixed vocabularies declare `choices:`. Domain formats (`:currency`, `:iso_country`, `:date`, `:timezone`) declare `format:`. A declaration the generator cannot compile fails a spec, not production.
- **`:any` is removed.** No production declaration uses it any more (the three counted by `6.0-json-preferences.md` are gone); only specs did, and they move to real types.
- **Type mapping:**
@@ -76,7 +76,7 @@ Preferences reach the API in two ways, and both expose the Ruby preferences DSL
```ruby
preference :amount_min, :decimal, money: true
-preference :channel_ids, :array, of: :id, prefix: :chan, default: []
+preference :channel_ids, :array, of: :id, model: 'Spree::Channel', scope: ->(rule) { rule.store.channels }, default: []
preference :country_codes, :array, of: :string, format: :iso_country, default: []
preference :match_policy, :string, choices: %w[any all none], default: 'any'
preference :amounts, :hash, keys: :currency, values: :money, default: {}
@@ -115,8 +115,8 @@ The generic preference form (rule, action, calculator and integration editors) r
## Migration Path
**PR A: declarations and generator (no wire change)**
-1. Add `of:`, `keys:`/`values:`, `money:`, `format:` and `prefix:` to `Preferable` declarations. Complete every `:array` and `:hash` declaration in core and providers. Resolve the `:any` declarations.
-2. Replace the ID-decoding `parse_on_set` lambdas with `of: :id, prefix:`.
+1. Add `of:`, `keys:`/`values:`, `money:`, `format:`, `model:` and `scope:` to `Preferable` declarations. Complete every `:array` and `:hash` declaration in core and providers. Resolve the `:any` declarations.
+2. Replace the ID-decoding `parse_on_set` lambdas with `of: :id, model:, scope:`.
3. Add `PreferenceSchema::JsonSchema` and a spec that fails for any registered subtype whose schema does not compile.
**PR B: contract (breaking)**
@@ -137,7 +137,7 @@ Verification per PR: `pnpm test:changed`, then `pnpm test:rspec core` and `pnpm
## Constraints on Current Work
- A new preference declares its full type: `of:` on arrays, `keys:`/`values:` on hashes, `money: true` on money, `choices:` on fixed vocabularies, `format:` for currencies, countries and dates. Never `:any`.
-- An ID list is `of: :id, prefix:`, never a `parse_on_set` decoding lambda.
+- An ID list is `of: :id, model:, scope:`, never a `parse_on_set` decoding lambda.
- Never add a `preferred_*` attribute to a serializer or a permitted-params list. Expose the setting under its plain name through the model.
- Never add `preference_schema` to a new serializer. Subtype schemas belong to the family's `/…/types` endpoint.
- A new configurable family publishes a `/…/types` endpoint and a typed union, with the fallback member, from day one.
diff --git a/packages/dashboard-core/src/components/preferences-form.tsx b/packages/dashboard-core/src/components/preferences-form.tsx
index a57e6b1040..216e814ad1 100644
--- a/packages/dashboard-core/src/components/preferences-form.tsx
+++ b/packages/dashboard-core/src/components/preferences-form.tsx
@@ -518,7 +518,13 @@ function NumberPreferenceInput({
if (Number.isInteger(number)) onChange(number)
return
}
- onChange(DECIMAL_REGEX.test(trimmed) ? trimmed : String(number))
+ // `.5` or `1e-7` is a number the server would refuse as typed; send its
+ // plain decimal form.
+ onChange(
+ DECIMAL_REGEX.test(trimmed)
+ ? trimmed
+ : number.toLocaleString('en-US', { useGrouping: false, maximumFractionDigits: 20 }),
+ )
}
return (
diff --git a/spree/api/lib/spree/api/openapi/schema_helper.rb b/spree/api/lib/spree/api/openapi/schema_helper.rb
index b029544095..d19a72b22e 100644
--- a/spree/api/lib/spree/api/openapi/schema_helper.rb
+++ b/spree/api/lib/spree/api/openapi/schema_helper.rb
@@ -35,6 +35,29 @@ module Spree
end
# Common schemas that are not from serializers
+ # The nested calculator of a promotion action and a delivery method,
+ # keyed by the preference family its `preferences` belong to.
+ CALCULATOR_SCHEMAS = {
+ 'PromotionCalculator' => [:PromotionActionCalculator, {
+ type: :object,
+ description: "The action's nested calculator (when the action carries one — null for actions like `free_shipping`)",
+ properties: {
+ type: { type: :string, example: 'flat_rate', description: 'Wire shorthand for the calculator subclass' },
+ preferences: { '$ref' => '#/components/schemas/PromotionCalculatorPreferences' }
+ },
+ required: %w[type preferences]
+ }],
+ 'DeliveryCalculator' => [:DeliveryMethodCalculator, {
+ type: :object,
+ description: 'How the delivery method prices a shipment',
+ properties: {
+ type: { type: :string, example: 'flat_rate', description: 'Wire shorthand for the calculator subclass' },
+ preferences: { '$ref' => '#/components/schemas/DeliveryCalculatorPreferences' }
+ },
+ required: %w[type preferences]
+ }]
+ }.freeze
+
def common_schemas
{
PaginationMeta: {
@@ -172,24 +195,6 @@ module Spree
additionalProperties: true,
example: { type: 'object', properties: { amount: { type: 'string', format: 'money', default: '0.0' } }, additionalProperties: false }
},
- PromotionActionCalculator: {
- type: :object,
- description: "The action's nested calculator (when the action carries one — null for actions like `free_shipping`)",
- properties: {
- type: { type: :string, example: 'flat_rate', description: 'Wire shorthand for the calculator subclass' },
- preferences: { '$ref' => '#/components/schemas/PromotionCalculatorPreferences' }
- },
- required: %w[type preferences]
- },
- DeliveryMethodCalculator: {
- type: :object,
- description: 'How the delivery method prices a shipment',
- properties: {
- type: { type: :string, example: 'flat_rate', description: 'Wire shorthand for the calculator subclass' },
- preferences: { '$ref' => '#/components/schemas/DeliveryCalculatorPreferences' }
- },
- required: %w[type preferences]
- },
PromotionActionLineItem: {
type: :object,
description: 'One row in a `create_line_items` action — the variant added to the order and how many',
@@ -583,6 +588,9 @@ module Spree
props[key] = { '$ref' => "#/components/schemas/#{family}Preferences" } if props&.key?(key)
end
+ # A calculator names its family's union, so it is published only
+ # alongside that union.
+ CALCULATOR_SCHEMAS.slice(*families).each_value { |name, schema| schemas[name] = schema }
patch_delivery_method_calculator(schemas)
schemas
end