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.

Move typelizer fix for flaky tests to test env (#14240)

Kacper Mękarski25ef6d4ea01c
diff --git a/spree/api/lib/spree/api.rb b/spree/api/lib/spree/api.rb
index 5ec86fa1fe..093d3c7b6b 100644
--- a/spree/api/lib/spree/api.rb
+++ b/spree/api/lib/spree/api.rb
@@ -4,7 +4,6 @@ require 'alba'
require 'oj'
require 'typelizer'
require 'typelizer/proc_resource_resolution'
-require 'typelizer/always_register_type_hints'
module Spree
module Api
diff --git a/spree/api/lib/spree/api/testing_support/always_register_type_hints.rb b/spree/api/lib/spree/api/testing_support/always_register_type_hints.rb
new file mode 100644
index 0000000000..87ccc28bf1
--- /dev/null
+++ b/spree/api/lib/spree/api/testing_support/always_register_type_hints.rb
@@ -0,0 +1,28 @@
+module Spree
+ module Api
+ module TestingSupport
+ # Typelizer only records its `typelize` hints when enabled at the moment the
+ # serializer class body runs (see `assign_type_information`'s `return unless
+ # Typelizer.enabled?`). Spree keeps Typelizer disabled at boot and only enables it
+ # while generating OpenAPI schemas on demand (see SchemaHelper#with_typelizer_enabled).
+ # A serializer autoloaded by an earlier spec while disabled would otherwise lose its
+ # hints, and Rswag schema validation would see `object` for the `id`/`created_at`/
+ # `updated_at` declared on the shared BaseSerializer.
+ #
+ # Record the metadata unconditionally so it survives regardless of load order. This
+ # only matters for the on-demand schema generation in the test suite, so it is
+ # installed from spec_helper rather than the gem's runtime load path.
+ module AlwaysRegisterTypeHints
+ def assign_type_information(attribute_name, attributes)
+ attributes.each do |name, attrs|
+ next unless name
+
+ store_type(attribute_name, name, Typelizer::TypeParser.parse_declaration(attrs))
+ end
+ end
+ end
+ end
+ end
+end
+
+Typelizer::DSL::ClassMethods.prepend(Spree::Api::TestingSupport::AlwaysRegisterTypeHints)
diff --git a/spree/api/lib/typelizer/always_register_type_hints.rb b/spree/api/lib/typelizer/always_register_type_hints.rb
deleted file mode 100644
index 843dc5cf7f..0000000000
--- a/spree/api/lib/typelizer/always_register_type_hints.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-module Typelizer
- module DSL
- # Spree generates OpenAPI schemas on demand and keeps Typelizer disabled at boot
- # (so types are not regenerated on every code reload). Upstream `typelize` only
- # records its type hints when Typelizer is enabled at the moment the serializer
- # class body runs — see `assign_type_information`'s `return unless Typelizer.enabled?`.
- # A serializer autoloaded while disabled (e.g. by an earlier spec) therefore loses
- # its hints, and schema generation later falls back to `object` for those attributes
- # (notably the `id`/`created_at`/`updated_at` declared on the shared BaseSerializer).
- #
- # Record the metadata unconditionally — it is harmless to store and is required for
- # on-demand schema generation. Boot-time file generation stays gated by `enabled?`.
- module AlwaysRegisterTypeHints
- def assign_type_information(attribute_name, attributes)
- attributes.each do |name, attrs|
- next unless name
-
- store_type(attribute_name, name, TypeParser.parse_declaration(attrs))
- end
- end
- end
-
- ClassMethods.prepend(AlwaysRegisterTypeHints)
- end
-end
diff --git a/spree/api/spec/spec_helper.rb b/spree/api/spec/spec_helper.rb
index bf5a038cd8..ae0c4a05b9 100644
--- a/spree/api/spec/spec_helper.rb
+++ b/spree/api/spec/spec_helper.rb
@@ -32,6 +32,10 @@ rescue LoadError
exit
end
+# Make Typelizer record `typelize` hints regardless of load order. Required here —
+# before any spec can autoload a serializer — so on-demand schema generation works.
+require 'spree/api/testing_support/always_register_type_hints'
+
require 'rspec/rails'
require 'database_cleaner/active_record'
require 'ffaker'