From 2370122e0b9edff479c0b5151c3964482ee8cd48 Mon Sep 17 00:00:00 2001 From: Filipe Lopes Date: Mon, 16 Nov 2026 17:19:21 -0300 Subject: [PATCH 1/2] fix(mapper): populate canonical url from algorithm metadata --- .../map-projects/MultiAlgoSelector.jsx | 7 ++----- .../__tests__/customAlgoMetadata.test.js | 17 +++++++++++++++++ src/components/map-projects/algorithms.jsx | 9 ++------- .../map-projects/customAlgoMetadata.js | 13 +++++++++++++ 4 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 src/components/map-projects/__tests__/customAlgoMetadata.test.js create mode 100644 src/components/map-projects/customAlgoMetadata.js diff --git a/src/components/map-projects/MultiAlgoSelector.jsx b/src/components/map-projects/MultiAlgoSelector.jsx index 2c7abf56..17fc3f2c 100644 --- a/src/components/map-projects/MultiAlgoSelector.jsx +++ b/src/components/map-projects/MultiAlgoSelector.jsx @@ -35,6 +35,7 @@ import orderBy from 'lodash/orderBy' import ConceptIcon from '../concepts/ConceptIcon' import { OperationsContext } from '../app/LayoutContext' import { isLikelyCanonicalUrl } from './algorithms' +import { metadataFields } from './customAlgoMetadata.js' import RepoVersionSearchAutocomplete from '../repos/RepoVersionSearchAutocomplete' import APIService from '../../services/APIService'; import { needsSessionToken, getTokenFlag, tokenInputValue, removeSavedToken } from '../../services/tokenScope'; @@ -389,11 +390,7 @@ export default function MultiAlgoSelector({ return; } - updateSelected(key, { - id: data.ID || data.id || '', - name: data.name || '', - description: data.description || '', - }); + updateSelected(key, metadataFields(data)); setCustomAlgoState(key, { isLoading: false, tokenRequired: false, diff --git a/src/components/map-projects/__tests__/customAlgoMetadata.test.js b/src/components/map-projects/__tests__/customAlgoMetadata.test.js new file mode 100644 index 00000000..499cfadb --- /dev/null +++ b/src/components/map-projects/__tests__/customAlgoMetadata.test.js @@ -0,0 +1,17 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import { metadataFields } from '../customAlgoMetadata.js' + +test('metadata supplies the canonical URL alongside the other form fields', () => { + assert.deepEqual(metadataFields({ ID: 'agent', name: 'Agent', canonical_url: 'http://example.test/system' }), { + id: 'agent', name: 'Agent', description: '', canonical_url: 'http://example.test/system' + }) +}) + +test('missing or invalid canonical URL leaves the existing field untouched', () => { + for (const canonical_url of [undefined, '', 'invalid', null]) { + assert.deepEqual(metadataFields({ id: 'agent', name: 'Agent', canonical_url }), { + id: 'agent', name: 'Agent', description: '' + }) + } +}) diff --git a/src/components/map-projects/algorithms.jsx b/src/components/map-projects/algorithms.jsx index 9f41c5e0..fbce5bc4 100644 --- a/src/components/map-projects/algorithms.jsx +++ b/src/components/map-projects/algorithms.jsx @@ -1,5 +1,6 @@ import React from 'react' import MatchingIcon from '@mui/icons-material/DeviceHub'; +import { isLikelyCanonicalUrl } from './customAlgoMetadata.js' // Canonical concept-identity config per algorithm type // (plans/unified-mapper-model.md). Single source of truth shared between @@ -47,13 +48,7 @@ export const CONCEPT_IDENTITY_BY_TYPE = { } } -// Cheap structural validator for canonical URLs. The unified mapper model -// requires every algorithm's concept_identity.canonical_url to be a URL — -// custom algos rely on user input for this, so the form validates here. -export const isLikelyCanonicalUrl = (value) => { - if(!value || typeof value !== 'string') return false - return /^https?:\/\/[^\s]+$/i.test(value.trim()) -} +export { isLikelyCanonicalUrl } // Walk algosSelected and return the subset that fails project-config // validation. Currently: custom algos must have a valid canonical_url. diff --git a/src/components/map-projects/customAlgoMetadata.js b/src/components/map-projects/customAlgoMetadata.js new file mode 100644 index 00000000..adc794d8 --- /dev/null +++ b/src/components/map-projects/customAlgoMetadata.js @@ -0,0 +1,13 @@ +// Match the form's URL validation when accepting metadata from a custom API. +export const isLikelyCanonicalUrl = value => { + if(!value || typeof value !== 'string') return false + return /^https?:\/\/[^\s]+$/i.test(value.trim()) +} + +// Omit an absent or invalid canonical so an existing form value is preserved. +export const metadataFields = data => ({ + id: data.ID || data.id || '', + name: data.name || '', + description: data.description || '', + ...(isLikelyCanonicalUrl(data.canonical_url) ? { canonical_url: data.canonical_url.trim() } : {}), +}) From 3198b26e7c21fa2c712c90d70bc2c5508ad977af Mon Sep 17 00:00:00 2001 From: Filipe Lopes Date: Mon, 16 Nov 2026 17:21:22 -0300 Subject: [PATCH 2/2] fix(mapper): retry algorithm metadata without operation marker --- .../map-projects/MultiAlgoSelector.jsx | 9 +-- .../__tests__/customAlgoMetadata.test.js | 58 ++++++++++++++++++- .../map-projects/customAlgoMetadata.js | 14 +++++ 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/src/components/map-projects/MultiAlgoSelector.jsx b/src/components/map-projects/MultiAlgoSelector.jsx index 17fc3f2c..dced75e2 100644 --- a/src/components/map-projects/MultiAlgoSelector.jsx +++ b/src/components/map-projects/MultiAlgoSelector.jsx @@ -35,7 +35,7 @@ import orderBy from 'lodash/orderBy' import ConceptIcon from '../concepts/ConceptIcon' import { OperationsContext } from '../app/LayoutContext' import { isLikelyCanonicalUrl } from './algorithms' -import { metadataFields } from './customAlgoMetadata.js' +import { metadataFields, fetchAlgorithmMetadata } from './customAlgoMetadata.js' import RepoVersionSearchAutocomplete from '../repos/RepoVersionSearchAutocomplete' import APIService from '../../services/APIService'; import { needsSessionToken, getTokenFlag, tokenInputValue, removeSavedToken } from '../../services/tokenScope'; @@ -354,9 +354,10 @@ export default function MultiAlgoSelector({ setCustomAlgoState(key, {isLoading: true, requestError: '', requestSuccess: '', tokenRequired: false}); const service = APIService.new(); - service.URL = selected.url; - - const response = await service.get(token || false, {}, undefined, true); + const response = await fetchAlgorithmMetadata(selected.url, url => { + service.URL = url; + return service.get(token || false, {}, undefined, true); + }); const status = response?.response?.status || response?.status; if([401, 403].includes(status)) { diff --git a/src/components/map-projects/__tests__/customAlgoMetadata.test.js b/src/components/map-projects/__tests__/customAlgoMetadata.test.js index 499cfadb..a2ae1900 100644 --- a/src/components/map-projects/__tests__/customAlgoMetadata.test.js +++ b/src/components/map-projects/__tests__/customAlgoMetadata.test.js @@ -1,6 +1,7 @@ import test from 'node:test' import assert from 'node:assert/strict' -import { metadataFields } from '../customAlgoMetadata.js' +import { createServer } from 'node:http' +import { metadataFields, fetchAlgorithmMetadata } from '../customAlgoMetadata.js' test('metadata supplies the canonical URL alongside the other form fields', () => { assert.deepEqual(metadataFields({ ID: 'agent', name: 'Agent', canonical_url: 'http://example.test/system' }), { @@ -15,3 +16,58 @@ test('missing or invalid canonical URL leaves the existing field untouched', () }) } }) + +test('metadata GET tries the operation without dollar first, then the original on failure', async () => { + const urls = [] + const result = await fetchAlgorithmMetadata('https://example.test/api/$match?x=$keep', async url => { + urls.push(url) + return urls.length === 1 ? {response: {status: 404}} : {data: {ID: 'x', name: 'X'}} + }) + assert.deepEqual(urls, ['https://example.test/api/match?x=$keep', 'https://example.test/api/$match?x=$keep']) + assert.equal(result.data.ID, 'x') +}) + +test('metadata GET avoids a retry on success or when the URL has no operation marker', async () => { + for (const url of ['https://example.test/api/$match/', 'https://example.test/api/match']) { + const calls = [] + await fetchAlgorithmMetadata(url, async candidate => { calls.push(candidate); return {data: {id: 'x', name: 'X'}} }) + assert.equal(calls.length, 1) + assert.equal(calls[0], 'https://example.test/api/match' + (url.endsWith('/') ? '/' : '')) + } +}) + +test('metadata GET retries when the first response is not usable metadata', async () => { + const calls = [] + const result = await fetchAlgorithmMetadata('https://example.test/$match', async url => { + calls.push(url) + return calls.length === 1 ? {data: {message: 'not metadata'}} : {data: {id: 'x', name: 'X'}} + }) + assert.equal(calls.length, 2) + assert.equal(result.data.id, 'x') +}) + +test('metadata GET retains an authentication failure if the alternate is missing', async () => { + const result = await fetchAlgorithmMetadata('https://example.test/$match', async url => ( + {response: {status: url.includes('$') ? 404 : 401}} + )) + assert.equal(result.response.status, 401) +}) + +test('metadata fallback performs real GET requests against a local server', async t => { + const requests = [] + const server = createServer((req, res) => { + requests.push([req.method, req.url, req.headers.authorization]) + res.setHeader('Content-Type', 'application/json') + if(req.url === '/$match') res.end(JSON.stringify({ID: 'x', name: 'X', canonical_url: 'https://example.test/system'})) + else { res.statusCode = 404; res.end('{}') } + }) + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)) + t.after(() => new Promise(resolve => server.close(resolve))) + const url = `http://127.0.0.1:${server.address().port}/$match` + const response = await fetchAlgorithmMetadata(url, async candidate => { + const result = await fetch(candidate, {headers: {Authorization: 'Token test'}}) + return result.ok ? {data: await result.json()} : {response: {status: result.status}} + }) + assert.deepEqual(requests, [['GET', '/match', 'Token test'], ['GET', '/$match', 'Token test']]) + assert.equal(metadataFields(response.data).canonical_url, 'https://example.test/system') +}) diff --git a/src/components/map-projects/customAlgoMetadata.js b/src/components/map-projects/customAlgoMetadata.js index adc794d8..9eb6f671 100644 --- a/src/components/map-projects/customAlgoMetadata.js +++ b/src/components/map-projects/customAlgoMetadata.js @@ -11,3 +11,17 @@ export const metadataFields = data => ({ description: data.description || '', ...(isLikelyCanonicalUrl(data.canonical_url) ? { canonical_url: data.canonical_url.trim() } : {}), }) + +// Try the non-operation metadata path before the original POST endpoint. +export const fetchAlgorithmMetadata = async (url, get) => { + const [path] = url.split(/[?#]/, 1) + const metadataUrl = path.replace(/\/\$([^/]+)(\/?)$/, '/$1$2') + url.slice(path.length) + const first = await get(metadataUrl) + const data = first?.data || first + if(metadataUrl === url || (!first?.response && data?.name && (data?.ID || data?.id))) return first + const second = await get(url) + const secondData = second?.data || second + if(!second?.response && secondData?.name && (secondData?.ID || secondData?.id)) return second + const firstStatus = first?.response?.status || first?.status + return [401, 403].includes(firstStatus) ? first : second +}