Module: Spree::Api::OpenAPI::SchemaHelper

Extended by:
SchemaHelper
Included in:
SchemaHelper
Defined in:
lib/spree/api/openapi/schema_helper.rb

Overview

Helper module to reference Typelizer-generated schemas in rswag specs

Instance Method Summary collapse

Instance Method Details

#admin_schemasObject

Get all admin schemas (Typelizer + common)



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/spree/api/openapi/schema_helper.rb', line 195

def admin_schemas
  schemas = common_schemas

  # Override AuthResponse for admin: reference AdminUser, and drop refresh_token from the body
  # (admin sets the refresh token as an HttpOnly cookie, not in the JSON response).
  schemas[:AuthResponse] = {
    type: :object,
    properties: {
      token: { type: :string, description: 'JWT access token' },
      user: { '$ref' => '#/components/schemas/AdminUser' }
    },
    required: %w[token user]
  }

  begin
    schemas.merge!(typelizer_schemas(:admin))
  rescue StandardError => e
    Rails.logger.warn "Failed to load Typelizer admin schemas: #{e.message}"
  end

  schemas
end

#all_schemasObject

Get all store schemas (Typelizer + common)



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/spree/api/openapi/schema_helper.rb', line 182

def all_schemas
  schemas = common_schemas

  begin
    schemas.merge!(typelizer_schemas(:store))
  rescue StandardError => e
    Rails.logger.warn "Failed to load Typelizer schemas: #{e.message}"
  end

  schemas
end

#common_schemasObject

Common schemas that are not from serializers



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/spree/api/openapi/schema_helper.rb', line 36

def common_schemas
  {
    PaginationMeta: {
      type: :object,
      properties: {
        page: { type: :integer, example: 1 },
        limit: { type: :integer, example: 25 },
        count: { type: :integer, example: 100, description: 'Total number of records' },
        pages: { type: :integer, example: 4, description: 'Total number of pages' },
        from: { type: :integer, example: 1, description: 'Index of first record on this page' },
        to: { type: :integer, example: 25, description: 'Index of last record on this page' },
        in: { type: :integer, example: 25, description: 'Number of records on this page' },
        previous: { type: :integer, nullable: true, example: nil, description: 'Previous page number' },
        next: { type: :integer, nullable: true, example: 2, description: 'Next page number' }
      },
      required: %w[page limit count pages from to in]
    },
    ErrorResponse: {
      type: :object,
      properties: {
        error: {
          type: :object,
          properties: {
            code: { type: :string, example: 'record_not_found' },
            message: { type: :string, example: 'Record not found' },
            details: {
              type: :object,
              description: 'Field-specific validation errors',
              nullable: true,
              example: { name: ['is too short', 'is required'], email: ['is invalid'] }
            }
          },
          required: %w[code message]
        }
      },
      required: %w[error],
      example: {
        error: {
          code: 'validation_error',
          message: 'Validation failed',
          details: { name: ['is too short'], email: ['is invalid'] }
        }
      }
    },
    AuthResponse: {
      type: :object,
      properties: {
        token: { type: :string, description: 'JWT access token' },
        refresh_token: { type: :string, description: 'Refresh token for obtaining new access tokens' },
        user: { '$ref' => '#/components/schemas/Customer' }
      },
      required: %w[token refresh_token user]
    },
    PermissionRule: {
      type: :object,
      description: 'A single permission rule (CanCanCan rule). Rules are applied in order, last-matching-wins.',
      properties: {
        allow: { type: :boolean, description: 'true for `can`, false for `cannot`' },
        actions: { type: :array, items: { type: :string }, description: 'Action names, e.g. ["read", "update"] or ["manage"]' },
        subjects: { type: :array, items: { type: :string }, description: 'Subject class names, e.g. ["Spree::Product"] or ["all"]' },
        has_conditions: { type: :boolean, description: 'True if the server-side rule has per-record conditions. The SPA shows the action optimistically and handles 403 from the API.' }
      },
      required: %w[allow actions subjects has_conditions]
    },
    MeResponse: {
      type: :object,
      description: 'Current admin user profile and serialized permissions',
      properties: {
        user: { '$ref' => '#/components/schemas/AdminUser' },
        permissions: { type: :array, items: { '$ref' => '#/components/schemas/PermissionRule' } }
      },
      required: %w[user permissions]
    },
    CheckoutRequirement: {
      type: :object,
      properties: {
        step: { type: :string, description: 'Checkout step this requirement belongs to', example: 'payment' },
        field: { type: :string, description: 'Field that needs to be satisfied', example: 'payment' },
        message: { type: :string, description: 'Human-readable requirement message', example: 'Add a payment method' }
      },
      required: %w[step field message]
    },
    CartWarning: {
      type: :object,
      description: 'A warning about a cart issue (e.g., item removed due to stock change)',
      properties: {
        code: { type: :string, description: 'Machine-readable warning code', example: 'line_item_removed' },
        message: { type: :string, description: 'Human-readable warning message', example: 'Blue T-Shirt was removed because it was sold out' },
        line_item_id: { type: :string, nullable: true, description: 'Prefixed line item ID (when applicable)', example: 'li_abc123' },
        variant_id: { type: :string, nullable: true, description: 'Prefixed variant ID (when applicable)', example: 'variant_abc123' }
      },
      required: %w[code message]
    },
    FulfillmentManifestItem: {
      type: :object,
      description: 'An item within a fulfillment — which line item and how many units are in this fulfillment',
      properties: {
        item_id: { type: :string, description: 'Line item ID', example: 'li_abc123' },
        variant_id: { type: :string, description: 'Variant ID', example: 'variant_abc123' },
        quantity: { type: :integer, description: 'Quantity in this fulfillment', example: 2 }
      },
      required: %w[item_id variant_id quantity]
    },
    AdminUserRoleAssignment: {
      type: :object,
      description: 'A role assignment for the current store on a staff member',
      properties: {
        id: { type: :string, description: 'Prefixed role ID', example: 'role_abc123' },
        name: { type: :string, description: 'Role name', example: 'admin' }
      },
      required: %w[id name]
    },
    PreferenceField: {
      type: :object,
      description: 'A single configurable preference on a payment method, promotion rule/action, or calculator. The frontend uses `type` + `default` to render a sensible input.',
      properties: {
        key: { type: :string, example: 'amount_min' },
        type: { type: :string, example: 'decimal', description: 'string | text | password | integer | decimal | boolean | array | hash' },
        default: { description: 'Default value (any JSON type), null when there is no default', nullable: true }
      },
      required: %w[key type]
    },
    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' },
        label: { type: :string, example: 'Flat Rate' },
        preferences: { type: :object, additionalProperties: true },
        preference_schema: { type: :array, items: { '$ref' => '#/components/schemas/PreferenceField' } }
      },
      required: %w[type label preferences preference_schema]
    },
    PromotionActionLineItem: {
      type: :object,
      description: 'One row in a `create_line_items` action — the variant added to the order and how many',
      properties: {
        variant_id: { type: :string, example: 'variant_abc123' },
        quantity: { type: :integer, example: 1 }
      },
      required: %w[variant_id quantity]
    }
  }
end

#error_responseObject

Error response schema



31
32
33
# File 'lib/spree/api/openapi/schema_helper.rb', line 31

def error_response
  ref('ErrorResponse')
end

#paginated(schema_name) ⇒ Object

Paginated response wrapper



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spree/api/openapi/schema_helper.rb', line 16

def paginated(schema_name)
  {
    type: :object,
    properties: {
      data: {
        type: :array,
        items: ref(schema_name)
      },
      meta: ref('PaginationMeta')
    },
    required: %w[data meta]
  }
end

#ref(schema_name) ⇒ Object

Reference a schema by name (e.g., ‘Product’, ‘Order’)



11
12
13
# File 'lib/spree/api/openapi/schema_helper.rb', line 11

def ref(schema_name)
  { '$ref' => "#/components/schemas/#{schema_name}" }
end