-
Notifications
You must be signed in to change notification settings - Fork 173
feat: Implement migration for deprecated client fields in configuration #1219
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,13 @@ import { | |
| ClientExpressConfiguration, | ||
| ClientOrganizationRequireBehaviorEnum, | ||
| } from 'auth0'; | ||
| import _ from 'lodash'; | ||
| import { Assets, Auth0APIClient } from '../../../types'; | ||
| import { paginate } from '../client'; | ||
| import DefaultAPIHandler from './default'; | ||
| import { getConnectionProfile } from './connectionProfiles'; | ||
| import { getUserAttributeProfiles } from './userAttributeProfiles'; | ||
| import log from '../../../logger'; | ||
|
|
||
| const multiResourceRefreshTokenPoliciesSchema = { | ||
| type: ['array', 'null'], | ||
|
|
@@ -316,6 +318,11 @@ export default class ClientHandler extends DefaultAPIHandler { | |
|
|
||
| assets.clients = await this.sanitizeMapExpressConfiguration(this.client, clients); | ||
|
|
||
| assets.clients = this.normalizeClientFields({ | ||
| clients, | ||
| fields: [{ newField: 'cross_origin_authentication', deprecatedField: 'cross_origin_auth' }], | ||
| }); | ||
|
|
||
| const excludedClients = (assets.exclude && assets.exclude.clients) || []; | ||
|
|
||
| const { del, update, create, conflicts } = await this.calcChanges(assets); | ||
|
|
@@ -373,10 +380,56 @@ export default class ClientHandler extends DefaultAPIHandler { | |
| is_global: false, | ||
| }); | ||
|
|
||
| this.existing = clients; | ||
| const sanitizedClients = this.normalizeClientFields({ | ||
| clients, | ||
| fields: [{ newField: 'cross_origin_authentication', deprecatedField: 'cross_origin_auth' }], | ||
| }); | ||
|
Comment on lines
+383
to
+386
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this logic under existing to |
||
|
|
||
| this.existing = sanitizedClients; | ||
| return this.existing; | ||
| } | ||
|
|
||
| /** | ||
| * @description Maps deprecated client fields to their new counterparts and removes the deprecated field. | ||
| * If a deprecated field exists, its value is always used for the new field, ensuring data migration | ||
| * and preventing loss of configuration data during schema transitions. | ||
|
Comment on lines
+393
to
+395
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic is not correctly handled.
Use the value of
Use the value of Always give preference to the new key |
||
| * @returns Client[] | ||
| */ | ||
| normalizeClientFields = ({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already a function named |
||
| clients, | ||
| fields, | ||
| }: { | ||
| clients: Client[]; | ||
| fields: { | ||
| newField: string; | ||
| deprecatedField: string; | ||
| }[]; | ||
| }): Client[] => | ||
| clients.map( | ||
| (client) => | ||
| fields.reduce( | ||
| (acc, { deprecatedField, newField }) => { | ||
| const hasDeprecated = _.has(acc, deprecatedField); | ||
| const hasNew = _.has(acc, newField); | ||
|
|
||
| if (hasDeprecated) { | ||
| // If deprecated exists and new is missing, log a warning and copy the value | ||
| if (!hasNew) { | ||
| log.warn( | ||
| `Client '${client.name}': The '${deprecatedField}' field is deprecated. Migrating value to '${newField}'.` | ||
| ); | ||
| acc[newField] = acc[deprecatedField]; | ||
| } | ||
| // Remove the deprecated field | ||
| return _.omit(acc, deprecatedField); | ||
| } | ||
|
|
||
| return acc; | ||
| }, | ||
| { ...client } as Record<string, unknown> | ||
| ) as Client | ||
| ); | ||
|
Comment on lines
+407
to
+431
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reduce complexity, read more on link |
||
|
|
||
| // convert names back to IDs for express configuration | ||
| async sanitizeMapExpressConfiguration( | ||
| auth0Client: Auth0APIClient, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please import functions instead of
_for lodash.