Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: handle edge case in toCamelCase FormatHelper #1792

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 32 additions & 0 deletions docs/migrations/version-3-to-4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Migration from v3 to v4
This document contain all the breaking changes and migrations guidelines for adapting your code to the new version.

## Property Naming changed for certain Edge Cases
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved

Object properties were formatted wrong when they contained a number followed by an underscore and a letter.
This has been fixed in this version, which might means properties might be renamed. If you encounter errors, check for properties that were renamed.
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved

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
Loading