Repository navigation
OpenConceptLab/ocl_issues#3008 | preview and convert resource-versioned references (API) - #1033
snyaggarwal wants to merge 6 commits into
Conversation
…n's resource-versioned references to repo-versioned or unversioned ones
snyaggarwal
left a comment
There was a problem hiding this comment.
Verdict: not ready to merge yet. One important finding and three nits on 7e49ecf.
What checks out:
- The swap: it's one transaction, it removes and then adds through the normal paths, and it rolls back fully if any add fails.
- Scope: HEAD only and editors only, and conversions go through the write checkpoint.
- Version picking: recorded, then as adding would pick, matching OpenConceptLab/ocl_issues#3005. A named version is kept.
- Kept options: the cascade and include or exclude are kept, and
transformis dropped. - Other cases: an expression already present only removes the old reference, and released versions are untouched.
- Load: lookups are batched per repository, and member URLs come from two queries.
The important finding (inline) is that the preview evaluates converted references with the original creator's access, while the conversion uses the requester's. That can show an editor members of a private source they can't view, and it lets the preview disagree with the result.
…uester; the preview says when unversioned creates a lock and caps member lists with counts
|
All four review points are fixed in the latest commit. The description is updated.
There are two new tests, so 17 in all. They pass locally with Redis unreachable, as in CI, and pylint is clean on the changed files. |
snyaggarwal
left a comment
There was a problem hiding this comment.
Verdict: ready to merge once CI passes. No findings on a3872a1.
All four fixes check out:
- Requester: converted references are built with the requester as
created_by, so the preview and the conversion evaluate with the same access, and an editor sees nothing from a source they can't view. The new test covers that. creates_lock: it's set onunversionedwhenever the resolved version isn't already the collection's lock.- List building:
plan()builds the reference list once. - Member lists: each names at most 25 members, with counts.
keeps_members,addedandremovedare computed on the full sets before the lists are capped.
I found nothing else across the PR. The tests also pass with Redis unreachable, so they don't depend on a broker in CI.
Still to do for OpenConceptLab/ocl_issues#3008, outside this PR: the TBv3 dialog, and the KB's "blocked" line for converting to repo-versioned.
…ly versions the collection's owner can view
|
Jon's point on OpenConceptLab/ocl_online#549 is addressed in c6ed52f. When no recorded version holds the resource version, the repo-versioned pick now chooses only among versions the collection's owner can view: public ones and the owner's own. A conversion can no longer pin the collection to another owner's private release. A new test covers it. The local Elasticsearch container is down, so CI is this commit's first test run; pylint is clean. |
paynejd
left a comment
There was a problem hiding this comment.
Review at 10b2e9d. An independent defect pass (Codex, high effort), with every finding checked against the code. The owner's-view point from this morning is fixed and not repeated.
Verdict: request changes, for point 1. HEAD-only and editors-only both hold, in the view and in the worker.
Must fix before merge
- A pinned reference with no
systemfails the whole batch.prime()callsself.resolve(reference.system, ...)unguarded (reference_conversions.py~41). Withsystem=None,resolve_reference_expressionreachescanonical_url_to_url_and_version(None), where'|' in Noneraises TypeError. Bothpreview()andplan()prime every reference before the unsupported check, so one such reference makes"*"previews and "convert all" fail for the whole collection. Production has collections with legacy references like that: the expression names a concept or mapping version (/orgs/CIEL/sources/CIEL/concepts/1065/5512158/), butsystemwas never filled in, and they hold no members. No toggle covers this endpoint. Master'sresolve_expression_system_versionguards withif self.system. Suggestion inline.
With the guard, these fall through toget_unsupported_reasonas "not resource-versioned", becauseis_resource_versionedneeds a system. A clearer reason would be "it has no system" (suggestion inline). A reference that names only value sets, which FHIR allows, is already left alone, and that's right: such an include lists no codes to convert. Please add a test that mixes a reference with no system into a"*"preview and anallconversion.
Worth fixing
2. already_present ignores cascade. plan() (~266-270) matches on expression and include only, and build_expression has no cascade in it. If the collection already has the converted reference without the old one's cascade, the old reference is deleted and its cascaded mappings drop out, while the preview said "keeps". Match on cascade too, and key the in-batch dict by (expression, include, cascade).
3. Pinned exclusions. Unpinned, an exclusion removes every version of the concept (__exclude_resources, collections/models.py ~2167-2177). But the preview compares only the exclusion's own matched set (~204-207), so it shows "keeps" for a change that can drop a member. Suggest reporting pinned exclusions as unsupported for now. Suggestion inline.
4. No check between preview and convert. The convert request carries {id, to}, and plan() re-resolves. A release published in between changes the result unseen. Accept an optional expected repo_version per conversion, which the preview already returns. Report not_converted ("changed since the preview") when it differs. TBv3 then sends it back.
5. is_visible should use is_publicly_viewable (~115-122), which also needs a public HEAD. Suggestion inline. It's the same rule as #1025 and #1032.
Minor
6. The unversioned target's repo_version (~206) can name a release the requester can't view, because default resolution has no access check. Treat a target the requester can't view as not possible.
7. The worker doesn't recheck edit access (tasks.py ~222). That matches add_references today. Optional: recheck in both.
8. A version label containing + resolves through unquote_plus to a space, so the reference is saved with no members and reported converted. Optional: in convert(), check that each added reference resolved to the planned version.
| self.expansion = collection.expansion | ||
| self.namespace = Expansion.get_system_version_namespace(collection.parent_url) | ||
| self._holders = {} | ||
| self._resolved = {} |
There was a problem hiding this comment.
Point 5: the per-version cache is_visible uses (apply with the is_visible suggestion).
| self._resolved = {} | |
| self._resolved = {} | |
| self._visible = {} |
| reference.collection = self.collection | ||
| if not reference.namespace: | ||
| reference.__dict__['resolution_namespace'] = self.namespace | ||
| system_version = self.resolve(reference.system, reference.resolution_namespace, reference.version) |
There was a problem hiding this comment.
Point 1: a reference with no system resolves to nothing instead of raising, as master's resolve_expression_system_version does.
| system_version = self.resolve(reference.system, reference.resolution_namespace, reference.version) | |
| system_version = self.resolve( | |
| reference.system, reference.resolution_namespace, reference.version) if reference.system else None |
|
|
||
| @staticmethod | ||
| def get_unsupported_reason(reference): | ||
| if not reference.is_resource_versioned: |
There was a problem hiding this comment.
Point 1: names the reason for a pinned reference with no system, before the resource-versioned check that needs one.
| if not reference.is_resource_versioned: | |
| if reference.code and reference.resource_version and not reference.system: | |
| return 'it has no system' | |
| if not reference.is_resource_versioned: |
| return 'it also names a value set' | ||
| if reference.filter: | ||
| return 'it has a filter' | ||
| return None |
There was a problem hiding this comment.
Point 3: reports a pinned exclusion as unsupported for now (every reference reaching this line is pinned).
| return None | |
| if not reference.include: | |
| return 'it is an exclusion' | |
| return None |
| def is_visible(self, repo_version): | ||
| """Whether the collection's owner can view it: a public version or the owner's own""" | ||
| from core.common.models import BaseModel | ||
| if repo_version.public_access in BaseModel.PUBLIC_ACCESS_VALUES: | ||
| return True | ||
| if self.collection.organization_id: | ||
| return repo_version.organization_id == self.collection.organization_id | ||
| return repo_version.user_id == self.collection.user_id |
There was a problem hiding this comment.
Point 5: uses the API's own view rule, is_publicly_viewable (its HEAD public too), once per version, as it reads the HEAD (needs the _visible line in __init__).
| def is_visible(self, repo_version): | |
| """Whether the collection's owner can view it: a public version or the owner's own""" | |
| from core.common.models import BaseModel | |
| if repo_version.public_access in BaseModel.PUBLIC_ACCESS_VALUES: | |
| return True | |
| if self.collection.organization_id: | |
| return repo_version.organization_id == self.collection.organization_id | |
| return repo_version.user_id == self.collection.user_id | |
| def is_visible(self, repo_version): | |
| """Whether the collection's owner can view it: a publicly viewable version (its HEAD public too) or the | |
| owner's own. Once per version, as is_publicly_viewable reads the HEAD""" | |
| if repo_version.id not in self._visible: | |
| if self.collection.organization_id: | |
| owned = repo_version.organization_id == self.collection.organization_id | |
| else: | |
| owned = repo_version.user_id == self.collection.user_id | |
| self._visible[repo_version.id] = owned or repo_version.is_publicly_viewable | |
| return self._visible[repo_version.id] |
…system and exclusions, respect cascades and the previewed version, and offer only versions viewable with their HEAD
|
Thanks, Jon. Your review's points are addressed in 1cb3c89, except point 8.
New tests:
The requester test now expects no unversioned offer. The local Elasticsearch container is down, so CI is the first run of these; pylint is clean on the changed files. |
paynejd
left a comment
There was a problem hiding this comment.
Thanks, Sunny. Every point I raised is met.
- Declining point 8 is fine: no version label uses
+or%today. Labels aren't validated, so one could appear later; that can be its own ticket if it ever matters.
One new blocker: a queued conversion's result is stored as Python text, not JSON (suggestion inline).
- Why:
convert_referencesis a plain task.AsyncTask.on_successstoresjson.dumps(retval), thenafter_returnoverwrites it withstr(retval)(core/tasks/models.py:174).result_allcan't parse that. - The effect: for any conversion that comes back queued (202),
/tasks/<id>/hands TBv3 a string, and oclweb3#111 can't read the outcome.add_referenceshas the same problem today. QueueOnce tasks don't, because celery_once'safter_returndoesn't call super. - The fix: the suggestion makes this task a
QueueOnceCustomTask, asexport_sourceis.perform_taskalready turns a duplicate into a 409. - The root cause,
Task.after_returnoverwriting every plain task's JSON, deserves its own ticket: fixing it globally changes whatadd_referencesand the importers store.
Smaller:
- Preview cost: the preview runs
has_view_access(requester)once per reference. For an org-private target, that's a membership query each time, up to 500 a page. Cache it per target, as_visibledoes. - HEAD labels (optional, with oclweb3#111): an unversioned conversion into a source with no release resolves to HEAD. The dialog labels it "Follows HEAD" when
chosen_byisHEAD, but not under a HEAD lock. Afollows_headflag fromversion.is_headon each conversion would let TBv3 label it the same way in both cases. It stays the default choice, since unversioned comes first.
| @app.task(bind=True) | ||
| def convert_references(self, user_id, collection_id, conversions): |
There was a problem hiding this comment.
Stores the result as JSON, so a queued conversion's caller can read it from /tasks/<id>/.
| @app.task(bind=True) | |
| def convert_references(self, user_id, collection_id, conversions): | |
| # QueueOnce, as export_source: its after_return replaces AsyncTask's, which stores a result as str(), so the | |
| # task's record keeps this one as JSON for a queued conversion's caller. The same request twice at once gets 409 | |
| @app.task(base=QueueOnceCustomTask, bind=True) | |
| def convert_references(self, user_id, collection_id, conversions): |
…ed as JSON, the requester's view is checked once per version, and conversions say when they follow HEAD
|
Thanks, Jon. All three points from your Nov 24 review are addressed in 12fa6a9.
Pylint is clean on the changed files. The local Elasticsearch container is down, so CI is the first run of the new tests. OpenConceptLab/oclweb3#111 now reads |
The API half of OpenConceptLab/ocl_issues#3008 (epic OpenConceptLab/ocl_issues#2942). Web half: OpenConceptLab/oclweb3#111, the TBv3 dialog that uses these endpoints and needs this PR deployed first. It lets a collection's editors see what converting its resource-versioned references would keep, and convert them in one step. This one is off master, independent of the expansion health stack.
Endpoints (HEAD only, editors only)
POST /<owner>/collections/<collection>/references/conversions/preview/with{"ids": [...]}(at most 500) or{"ids": "*", "offset": n}(every resource-versioned reference, 500 per page). It changes nothing.For each reference, the response gives:
repo_versionedandunversioned):named,recorded,lock,release,HEAD,latest);keeps_members: whether those match today's members.creates_lock, onunversionedonly: when no lock exists for the repo, adding a version-less reference makes the version it resolved to the collection's lock.Each member list names at most 25 members, and its
_countsays how many there are. Everything is evaluated as the requester, as the conversion will be, so an editor never sees members of a source they can't view.The
summarygivesrepo_versioned_keeps_allandunversioned_keeps_all, so TBv3 can offer a one-step conversion. It also givestotal,offsetandnext_offsetfor paging through"*".POST /<owner>/collections/<collection>/references/conversions/with{"conversions": [{"id": ..., "to": "repo_versioned" | "unversioned"}]}or{"all": "repo_versioned" | "unversioned"}. It runs as a task, so?async=trueworks for large collections. Each result is one of:converted, with the new reference's id;already_present: the converted expression was already in the collection, so only the old reference goes;not_converted, with a reason;rolled_back.The rules, as decided on Nov 23
transform, which would otherwise make resource-versioned references again.Load
"*"previews are capped at 500 references per request.Tests
core/collections/tests/tests_reference_conversions.pyhas 17 tests:"*";all;already_present;They pass locally with Redis unreachable, as in CI.
🤖 Generated with Claude Code