deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

Deserialization/serialization naming strategy only gets run on root type properties

Open marcus-sa opened this issue 3 years ago • 1 comments

It seems that renaming properties during deserialization/serializations only works on the root type:

const camelCaseToSnakeCase = (str: string) =>
  str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);

class CamelCaseToSnakeCaseNamingStrategy extends NamingStrategy {
  constructor() {
    super('snake-case-to-camel-case');
  }

  override getPropertyName(
    type: TypeProperty | TypePropertySignature,
    forSerializer: string
  ): string | undefined {
    const propertyName = super.getPropertyName(type, forSerializer);
    return propertyName ? camelCaseToSnakeCase(propertyName) : undefined;
  }
}
class Post {
  id: number;
  likesCount: number;
}

class User {
  id: number;
  posts: Post[];
}

// likes_count doesn't get renamed to likesCount
deserialize<User>(
  {
    id: 1,
    posts: [
      {
        id: 1,
        likes_count: 3,
      }
    ],
  },
  undefined,
  serializer,
  new CamelCaseToSnakeCaseNamingStrategy()
);

marcus-sa avatar Jul 18 '22 08:07 marcus-sa

I've added with the fix of #311 a test for your use-case, too, which indicates that this works as expected. Please see https://github.com/deepkit/deepkit-framework/commit/aa19cabaf06219d2c453667b09600bcb9f7be44f#diff-75b4ae9bc8d02aed72eb4501bb111f1931e386147b1754a6c325fe0a59ad1815R907 and here another with your exact code https://github.com/deepkit/deepkit-framework/commit/81d998c535b53cafb806e14285df1cb261db2933

marcj avatar Jul 18 '22 14:07 marcj

Closing since last information indicates that this is fixed or a non-issue.

marcj avatar Dec 03 '22 11:12 marcj