Skip to content

Commit 01e8768

Browse files
authored
fix(Designer): Fixed issue where connection references would sometimes overlap (#4290)
* Fixed issue where connection references would sometimes overlap * Undefined catch * Fixed issue * Another fix * Fixed small ui issue
1 parent e61a878 commit 01e8768

File tree

5 files changed

+70
-45
lines changed

5 files changed

+70
-45
lines changed

apps/designer-standalone/src/app/AzureLogicAppsDesigner/laDesigner.tsx

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,24 @@ const DesignerEditor = () => {
182182
definition,
183183
};
184184

185-
const referencesToAdd = { ...(connectionsData?.managedApiConnections ?? {}) };
186-
if (Object.keys(connectionReferences ?? {}).length) {
185+
const newManagedApiConnections = { ...(connectionsData?.managedApiConnections ?? {}) };
186+
const newServiceProviderConnections: Record<string, any> = {};
187+
188+
const referenceKeys = Object.keys(connectionReferences ?? {});
189+
if (referenceKeys.length) {
187190
await Promise.all(
188-
Object.keys(connectionReferences).map(async (referenceKey) => {
191+
referenceKeys.map(async (referenceKey) => {
189192
const reference = connectionReferences[referenceKey];
190-
if (isArmResourceId(reference.connection.id) && !referencesToAdd[referenceKey]) {
193+
if (isArmResourceId(reference?.connection?.id) && !newManagedApiConnections[referenceKey]) {
194+
// Managed API Connection
191195
const {
192196
api: { id: apiId },
193197
connection: { id: connectionId },
194198
connectionProperties,
195199
} = reference;
196200
const connection = await getConnectionStandard(connectionId);
197201
const userIdentity = connectionProperties?.authentication?.identity;
198-
referencesToAdd[referenceKey] = {
202+
const newConnectionObj = {
199203
api: { id: apiId },
200204
connection: { id: connectionId },
201205
authentication: {
@@ -205,10 +209,22 @@ const DesignerEditor = () => {
205209
connectionRuntimeUrl: connection?.properties?.connectionRuntimeUrl ?? '',
206210
connectionProperties,
207211
};
212+
newManagedApiConnections[referenceKey] = newConnectionObj;
213+
} else if (reference?.connection?.id.startsWith('/serviceProviders/')) {
214+
// Service Provider Connection
215+
const connectionKey = reference.connection.id.split('/').splice(-1)[0];
216+
// We can't apply this directly in case there is a temporary key overlap
217+
// We need to move the data out to a new object, delete the old data, then apply the new data at the end
218+
newServiceProviderConnections[referenceKey] = connectionsData?.serviceProviderConnections?.[connectionKey];
219+
delete connectionsData?.serviceProviderConnections?.[connectionKey];
208220
}
209221
})
210222
);
211-
(connectionsData as ConnectionsData).managedApiConnections = referencesToAdd;
223+
(connectionsData as ConnectionsData).managedApiConnections = newManagedApiConnections;
224+
(connectionsData as ConnectionsData).serviceProviderConnections = {
225+
...connectionsData?.serviceProviderConnections,
226+
...newServiceProviderConnections,
227+
};
212228
}
213229

214230
const connectionsToUpdate = getConnectionsToUpdate(originalConnectionsData, connectionsData ?? {});
@@ -556,57 +572,63 @@ const addOrUpdateAppSettings = (settings: Record<string, string>, originalSettin
556572
return originalSettings;
557573
};
558574

575+
const hasNewKeys = (original: Record<string, any> = {}, updated: Record<string, any> = {}) => {
576+
return !Object.keys(updated).some((key) => !Object.keys(original).includes(key));
577+
};
578+
559579
const getConnectionsToUpdate = (
560580
originalConnectionsJson: ConnectionsData,
561581
connectionsJson: ConnectionsData
562582
): ConnectionsData | undefined => {
563-
const originalKeys = Object.keys({
564-
...(originalConnectionsJson.functionConnections ?? {}),
565-
...(originalConnectionsJson.apiManagementConnections ?? {}),
566-
...(originalConnectionsJson.managedApiConnections ?? {}),
567-
...(originalConnectionsJson.serviceProviderConnections ?? {}),
568-
});
569-
570-
const updatedKeys = Object.keys({
571-
...(connectionsJson.functionConnections ?? {}),
572-
...(connectionsJson.apiManagementConnections ?? {}),
573-
...(connectionsJson.managedApiConnections ?? {}),
574-
...(connectionsJson.serviceProviderConnections ?? {}),
575-
});
583+
const hasNewFunctionKeys = hasNewKeys(originalConnectionsJson.functionConnections, connectionsJson.functionConnections);
584+
const hasNewApimKeys = hasNewKeys(originalConnectionsJson.apiManagementConnections, connectionsJson.apiManagementConnections);
585+
const hasNewManagedApiKeys = hasNewKeys(originalConnectionsJson.managedApiConnections, connectionsJson.managedApiConnections);
586+
const hasNewServiceProviderKeys = hasNewKeys(
587+
originalConnectionsJson.serviceProviderConnections,
588+
connectionsJson.serviceProviderConnections
589+
);
576590

577-
// NOTE: We don't edit connections from the workflow, so existing connections should not be changed. If no new connections are added, there was no change.
578-
if (!updatedKeys.some((conn) => !originalKeys.includes(conn))) {
591+
if (!hasNewFunctionKeys && !hasNewApimKeys && !hasNewManagedApiKeys && !hasNewServiceProviderKeys) {
579592
return undefined;
580593
}
581594

582595
const connectionsToUpdate = { ...connectionsJson };
583-
for (const functionConnectionName of Object.keys(connectionsJson.functionConnections ?? {})) {
584-
if (originalConnectionsJson.functionConnections?.[functionConnectionName]) {
585-
(connectionsToUpdate.functionConnections as any)[functionConnectionName] =
586-
originalConnectionsJson.functionConnections[functionConnectionName];
596+
597+
if (hasNewFunctionKeys) {
598+
for (const functionConnectionName of Object.keys(connectionsJson.functionConnections ?? {})) {
599+
if (originalConnectionsJson.functionConnections?.[functionConnectionName]) {
600+
(connectionsToUpdate.functionConnections as any)[functionConnectionName] =
601+
originalConnectionsJson.functionConnections[functionConnectionName];
602+
}
587603
}
588604
}
589605

590-
for (const apimConnectionName of Object.keys(connectionsJson.apiManagementConnections ?? {})) {
591-
if (originalConnectionsJson.apiManagementConnections?.[apimConnectionName]) {
592-
(connectionsToUpdate.apiManagementConnections as any)[apimConnectionName] =
593-
originalConnectionsJson.apiManagementConnections[apimConnectionName];
606+
if (hasNewApimKeys) {
607+
for (const apimConnectionName of Object.keys(connectionsJson.apiManagementConnections ?? {})) {
608+
if (originalConnectionsJson.apiManagementConnections?.[apimConnectionName]) {
609+
(connectionsToUpdate.apiManagementConnections as any)[apimConnectionName] =
610+
originalConnectionsJson.apiManagementConnections[apimConnectionName];
611+
}
594612
}
595613
}
596614

597-
for (const managedApiConnectionName of Object.keys(connectionsJson.managedApiConnections ?? {})) {
598-
if (originalConnectionsJson.managedApiConnections?.[managedApiConnectionName]) {
599-
// eslint-disable-next-line no-param-reassign
600-
(connectionsJson.managedApiConnections as any)[managedApiConnectionName] =
601-
originalConnectionsJson.managedApiConnections[managedApiConnectionName];
615+
if (hasNewManagedApiKeys) {
616+
for (const managedApiConnectionName of Object.keys(connectionsJson.managedApiConnections ?? {})) {
617+
if (originalConnectionsJson.managedApiConnections?.[managedApiConnectionName]) {
618+
// eslint-disable-next-line no-param-reassign
619+
(connectionsJson.managedApiConnections as any)[managedApiConnectionName] =
620+
originalConnectionsJson.managedApiConnections[managedApiConnectionName];
621+
}
602622
}
603623
}
604624

605-
for (const serviceProviderConnectionName of Object.keys(connectionsJson.serviceProviderConnections ?? {})) {
606-
if (originalConnectionsJson.serviceProviderConnections?.[serviceProviderConnectionName]) {
607-
// eslint-disable-next-line no-param-reassign
608-
(connectionsJson.serviceProviderConnections as any)[serviceProviderConnectionName] =
609-
originalConnectionsJson.serviceProviderConnections[serviceProviderConnectionName];
625+
if (hasNewServiceProviderKeys) {
626+
for (const serviceProviderConnectionName of Object.keys(connectionsJson.serviceProviderConnections ?? {})) {
627+
if (originalConnectionsJson.serviceProviderConnections?.[serviceProviderConnectionName]) {
628+
// eslint-disable-next-line no-param-reassign
629+
(connectionsJson.serviceProviderConnections as any)[serviceProviderConnectionName] =
630+
originalConnectionsJson.serviceProviderConnections[serviceProviderConnectionName];
631+
}
610632
}
611633
}
612634

libs/designer/src/lib/core/queries/connections.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ export const getConnection = async (connectionId: string, connectorId: string, f
8888
return !connection && fetchResourceIfNeeded ? getConnectionFromResource(connectionId) : connection;
8989
};
9090

91-
export const getUniqueConnectionName = async (connectorId: string): Promise<string> => {
91+
export const getUniqueConnectionName = async (connectorId: string, existingKeys: string[] = []): Promise<string> => {
9292
const connectionNames = (await getConnectionsForConnector(connectorId)).map((connection) => connection.name);
9393
const connectorName = connectorId.split('/').at(-1);
94-
return ConnectionService().getUniqueConnectionName(connectorId, connectionNames, connectorName as string);
94+
return ConnectionService().getUniqueConnectionName(connectorId, [...connectionNames, ...existingKeys], connectorName as string);
9595
};
9696

9797
export const useConnectionResource = (connectionId: string) => {

libs/designer/src/lib/ui/Controls.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const CustomControls = () => {
3636
return (
3737
<Controls showInteractive={false}>
3838
<ControlButton id={searchId} aria-label={searchAria} title={searchAria} onClick={searchToggleClick}>
39-
<Icon iconName={'Search'} styles={iconStyles} />
39+
<Icon iconName={'Search'} />
4040
</ControlButton>
4141
<ControlButton aria-label={minimapAria} title={minimapAria} onClick={minimapToggleClick}>
4242
<Icon iconName={'Nav2DMapView'} styles={iconStyles} />

libs/designer/src/lib/ui/panel/connectionsPanel/createConnection/createConnectionWrapper.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export const CreateConnectionWrapper = () => {
6262
const availableGateways = useMemo(() => gatewaysQuery.data, [gatewaysQuery]);
6363
const gatewayServiceConfig = useGatewayServiceConfig();
6464

65+
const existingReferences = useSelector((state: RootState) => Object.keys(state.connections.connectionReferences));
66+
6567
const identity = WorkflowService().getAppIdentity?.() as ManagedIdentity;
6668

6769
const [isLoading, setIsLoading] = useState(false);
@@ -200,7 +202,7 @@ export const CreateConnectionWrapper = () => {
200202

201203
let connection, err;
202204

203-
const newName = await getUniqueConnectionName(connector.id);
205+
const newName = await getUniqueConnectionName(connector.id, existingReferences);
204206
if (isOAuthConnection) {
205207
await ConnectionService()
206208
.createAndAuthorizeOAuthConnection(newName, connector?.id ?? '', connectionInfo, parametersMetadata)
@@ -245,6 +247,7 @@ export const CreateConnectionWrapper = () => {
245247
closeConnectionsFlow,
246248
nodeIds,
247249
applyNewConnection,
250+
existingReferences,
248251
]
249252
);
250253

libs/logic-apps-shared/src/utils/src/lib/helpers/connections.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ function _isConnectionParameterHidden(connectionParameter: ConnectionParameter):
202202
}
203203

204204
export const getUniqueName = (keys: string[], prefix: string): { name: string; index: number } => {
205-
const set = new Set(keys.map((name) => name.split('::')[0]));
205+
const set = new Set(keys.map((name) => name.split('::')[0].toLowerCase()));
206206

207207
let index = 0;
208208
let name = prefix;
209-
while (set.has(name)) {
209+
while (set.has(name.toLowerCase())) {
210210
name = `${prefix}-${++index}`;
211211
}
212212

0 commit comments

Comments
 (0)