Skip to content

Commit

Permalink
fix!: handle edge case in toCamelCase FormatHelper (#1792)
Browse files Browse the repository at this point in the history
  • Loading branch information
marakalwa authored Feb 14, 2024
1 parent 9012c1c commit 26bb9ba
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
31 changes: 31 additions & 0 deletions docs/migrations/version-3-to-4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Migration from v3 to v4
This document contain all the breaking changes and migrations guidelines for adapting your code to the new version.

## Fixed edge cases for camel case names

Naming such as object properties using camel case formatting had an edge case where if they contained a number followed by an underscore and a letter it would be incorrectly formatted. This has been fixed in this version, which might mean properties, model names, etc that use camel case might be renamed.

This example contains such a string:

```yaml
type: object
properties:
aa_00_testAttribute:
type: string
```
This used to generate:
```ts
interface AnonymousSchema_1 {
aa_00TestAttribute?: string;
}
```

but will now generate:

```ts
interface AnonymousSchema_1 {
aa_00_testAttribute?: string;
}
```
15 changes: 14 additions & 1 deletion src/helpers/FormatHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,20 @@ export class FormatHelpers {
* @param {string} value to transform
* @returns {string}
*/
static toCamelCase = camelCase;
static toCamelCase(renderName: string): string {
const splt = renderName.split(/([0-9]_)/g);
if (splt.length > 1) {
return splt
.map((part) => {
if (part.match(/[0-9]_/g)) {
return part;
}
return camelCase(part);
})
.join('');
}
return camelCase(renderName);
}

/**
* Transform into a string of capitalized words without separators.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ describe('PropertyKeyConstrainer', () => {
const constrainedKey = constrainPropertyName('some weird_value!"#2');
expect(constrainedKey).toEqual('someWeirdValueExclamationQuotationHash_2');
});
test('should correctly handle casing with numbers', () => {
const constrainedKey = constrainPropertyName('aa_00_testAttribute');
expect(constrainedKey).toEqual('aa_00_testAttribute');
});
test('should not contain duplicate properties', () => {
const objectModel = new ObjectModel('test', undefined, {}, {});
const constrainedObjectModel = new ConstrainedObjectModel(
Expand Down

0 comments on commit 26bb9ba

Please sign in to comment.