Skip to content

Commit

Permalink
Support transform keys for OjSerializers & AMS
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Feb 27, 2025
1 parent c77a41a commit a556a68
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning].

### Added

- Support Alba transform keys (see [Alba docs](https://github.com/okuramasafumi/alba?tab=readme-ov-file#key-transformation)). ([@patvice])
- Support transform keys. ([@patvice, @skryukov])

- Support for deprecated attributes. ([@Envek])

Expand Down
3 changes: 2 additions & 1 deletion lib/typelizer/serializer_plugins/ams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def typelize_method_transform(method:, name:, binding:, type:, attrs:)
def properties
serializer._attributes_data.merge(serializer._reflections).flat_map do |key, association|
type = association.options[:serializer] ? Interface.new(serializer: association.options[:serializer]) : nil
adapter = ActiveModelSerializers::Adapter.configured_adapter
Property.new(
name: key.to_s,
name: adapter.transform_key_casing!(key.to_s, association.options),
type: type,
optional: association.options.key?(:if) || association.options.key?(:unless),
multi: association.respond_to?(:collection?) && association.collection?,
Expand Down
6 changes: 5 additions & 1 deletion lib/typelizer/serializer_plugins/oj_serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def methods_to_typelize
end

def properties
serializer._attributes
transform_keys = serializer.try(:_transform_keys)
attributes = serializer._attributes
attributes = attributes.transform_keys(&transform_keys) if transform_keys

attributes
.flat_map do |key, options|
if options[:association] == :flat
Interface.new(serializer: options.fetch(:serializer)).properties
Expand Down
16 changes: 16 additions & 0 deletions spec/__snapshots__/AmsTransformKeys.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Typelizer digest 33937e09714b42648ac9414bb04c6834
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AmsUser, AmsPost} from '@/types'

type AmsTransformKeys = {
/** Unique identifier */
id: string | null;
username: string;
active: boolean;
createdAt: string;
invitor: AmsUser;
posts: Array<AmsPost>;
}

export default AmsTransformKeys;
15 changes: 15 additions & 0 deletions spec/__snapshots__/OjSerializersTransformKeys.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Typelizer digest 6395b48daf28e8585e1bac96f227a242
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {OjSerializersUser, OjSerializersPost} from '@/types'

type OjSerializersTransformKeys = {
id: number;
username: string;
active: boolean;
invitor: OjSerializersUser;
posts: Array<OjSerializersPost>;
createdAt: string;
}

export default OjSerializersTransformKeys;
4 changes: 3 additions & 1 deletion spec/__snapshots__/index.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest afd77645c65867552ccc98f9e92071a4
// Typelizer digest 595dcb8625a1239161618071ca1c1e85
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
export type { default as AlbaInline } from './AlbaInline'
Expand All @@ -13,13 +13,15 @@ export type { default as AlbaUser } from './AlbaUser'
export type { default as AlbaUserSerializerFoo } from './AlbaUserSerializerFoo'
export type { AlbaVerbatimModuleSyntax } from './AlbaVerbatimModuleSyntax'
export type { default as AmsPost } from './AmsPost'
export type { default as AmsTransformKeys } from './AmsTransformKeys'
export type { default as AmsUserAuthor } from './AmsUserAuthor'
export type { default as AmsUserEmptyNested } from './AmsUserEmptyNested'
export type { default as AmsUser } from './AmsUser'
export type { default as AmsUserSerializerFoo } from './AmsUserSerializerFoo'
export type { AmsVerbatimModuleSyntax } from './AmsVerbatimModuleSyntax'
export type { default as OjSerializersFlatUser } from './OjSerializersFlatUser'
export type { default as OjSerializersPost } from './OjSerializersPost'
export type { default as OjSerializersTransformKeys } from './OjSerializersTransformKeys'
export type { default as OjSerializersUserAuthor } from './OjSerializersUserAuthor'
export type { default as OjSerializersUserEmptyNested } from './OjSerializersUserEmptyNested'
export type { default as OjSerializersUser } from './OjSerializersUser'
Expand Down
5 changes: 5 additions & 0 deletions spec/app/app/serializers/ams/transform_keys_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Ams
class TransformKeysSerializer < UserSerializer
attribute :created_at, key_transform: :camel_lower
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module OjSerializers
class TransformKeysSerializer < UserSerializer
attribute :created_at

transform_keys :camel_case
end
end

0 comments on commit a556a68

Please sign in to comment.