Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ jobs:
- 5432:5432

runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
name: Ruby ${{ matrix.ruby }}${{ matrix.panko && format(' (Panko {0})', matrix.panko) || '' }}
env:
BUNDLE_JOBS: 4
BUNDLE_RETRY: 3
DATABASE_URL: postgres://postgres:postgres@localhost:5432/typelizer_test_app_test
PANKO_VERSION: ${{ matrix.panko || '>= 0' }}
RAILS_VERSION: ${{ matrix.rails || '~> 7.1.3' }}
strategy:
matrix:
ruby:
Expand All @@ -51,6 +53,13 @@ jobs:
- "3.2"
- "3.1"
- "3.0"
panko:
- "" # latest version the Ruby and Rails versions resolve to
include:
# Panko 1.0 requires Ruby >= 3.4 and Rails >= 7.2
- ruby: "4.0"
panko: "~> 1.0"
rails: "~> 7.2.0"

steps:
- uses: actions/checkout@v6
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning].

## [Unreleased]

### Fixed

- Panko 1.0 support. Panko 1.0 no longer has the internal descriptor the plugin read, so it now uses the public `Panko::Descriptor`. ([@benjaminwols])

## [0.14.0] - 2026-10-03

### Added
Expand Down Expand Up @@ -515,6 +519,7 @@ and this project adheres to [Semantic Versioning].

- Initial release ([@skryukov])

[@benjaminwols]: https://github.com/benjaminwols
[@davidrunger]: https://github.com/davidrunger
[@Envek]: https://github.com/Envek
[@hkamberovic]: https://github.com/hkamberovic
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ gem "active_model_serializers"

gem "alba"

gem "panko_serializer"
gem "panko_serializer", ENV.fetch("PANKO_VERSION", ">= 0")

gem "blueprinter"

# Rails app
gem "rails", "~> 7.1.3"
gem "rails", ENV.fetch("RAILS_VERSION", "~> 7.1.3")
gem "pg", "~> 1.1"
gem "puma", ">= 5.0"
gem "tzinfo-data", platforms: %i[windows jruby]
Expand Down
76 changes: 54 additions & 22 deletions lib/typelizer/serializer_plugins/panko.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,78 @@ module Typelizer
module SerializerPlugins
class Panko < Base
def properties
descriptor = serializer.new.instance_variable_get(:@descriptor)
attributes = descriptor.attributes
methods_attributes = descriptor.method_fields
has_many_associations = descriptor.has_many_associations
has_one_associations = descriptor.has_one_associations

attributes.map do |att|
attribute_property(att)
end + methods_attributes.map do |att|
attribute_property(att)
end + has_many_associations.map do |assoc|
association_property(assoc, multi: true)
end + has_one_associations.map do |assoc|
association_property(assoc, multi: false)
if public_descriptor?
properties_from_public_descriptor
else
properties_from_internal_descriptor
end
end

private

def attribute_property(att)
# Panko >= 1.0 exposes a public, read-only Panko::Descriptor
def public_descriptor?
defined?(::Panko::Descriptor) && ::Panko::Descriptor.respond_to?(:for)
end

def properties_from_public_descriptor
descriptor = ::Panko::Descriptor.for(serializer)

attributes = (descriptor.attributes + descriptor.method_attributes).map do |att|
build_attribute_property(name: att.name.to_s, column_name: (att.source || att.name).to_s)
end

# has_many before has_one, matching the property order of Panko < 1.0
has_many, has_one = descriptor.associations.partition { |assoc| assoc.kind == :has_many }
associations = (has_many + has_one).map do |assoc|
build_association_property(
name: assoc.name.to_s,
serializer: assoc.descriptor.serializer,
multi: assoc.kind == :has_many
)
end

attributes + associations
end

# Panko < 1.0 keeps its descriptor in an instance variable of the C extension
def properties_from_internal_descriptor
descriptor = serializer.new.instance_variable_get(:@descriptor)

attributes = (descriptor.attributes + descriptor.method_fields).map do |att|
build_attribute_property(name: att.alias_name || att.name, column_name: att.name)
end

has_many = descriptor.has_many_associations.map do |assoc|
build_association_property(name: assoc.name_str, serializer: assoc.descriptor.type, multi: true)
end

has_one = descriptor.has_one_associations.map do |assoc|
build_association_property(name: assoc.name_str, serializer: assoc.descriptor.type, multi: false)
end

attributes + has_many + has_one
end

def build_attribute_property(name:, column_name:)
Property.new(
name: att.alias_name || att.name,
name: name,
optional: false,
nullable: false,
multi: false,
column_name: att.name
column_name: column_name
)
end

def association_property(assoc, multi: false)
key = assoc.name_str
serializer = assoc.descriptor.type
def build_association_property(name:, serializer:, multi:)
type = serializer ? context.interface_for(serializer) : nil
Property.new(
name: key,
name: name,
type: type,
optional: false,
nullable: false,
multi: multi,
column_name: key
column_name: name
)
end
end
Expand Down
Loading