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

#all_schemasObject

Get all schemas (Typelizer + common)



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/spree/api/openapi/schema_helper.rb', line 123

def all_schemas
  schemas = common_schemas

  begin
    schemas.merge!(typelizer_schemas)
  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
# 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]
    },
    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 prefixed ID', example: 'li_abc123' },
        variant_id: { type: :string, description: 'Variant prefixed ID', example: 'variant_abc123' },
        quantity: { type: :integer, description: 'Quantity in this fulfillment', example: 2 }
      },
      required: %w[item_id 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