# Bug OP-19928 - GET .../wiki_page_links returns 500 (invalid SQL) whenever a work package has at least one wiki page link

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-19928

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | High |
| Module | Modules and project features / Wiki, API |
| Target versions | 18.0.0 |
| Observed in versions |  |
| Sprint |  |
| Backlog bucket |  |
| Bug found in version (deprecated) | unreleased/dev |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers | Jan Sandbrink |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

### Before filing a report

Confirmed reproducible on the latest available patch releases of the 17.6 and 17.7 minor lines (17.6.0 and 17.7.1, the latest at the time of testing) via direct Rails-console reproduction, not just via the REST API. Searched community.openproject.org for existing reports (by &quot;wiki page link&quot;, &quot;PageLinkMetadataService&quot;, &quot;wiki\_page\_links&quot;, &quot;page\_links&quot;, &quot;relation page link&quot;) — found no existing report covering this.

### Steps to reproduce

1.  On any OpenProject 16.6/17.6/17.7 instance with the Wiki module enabled on a project, create at least one work package in that project.
    
2.  Create a `Wikis::RelationPageLink` linking that work package to any wiki page (e.g. via `POST /api/v3/work_packages/{id}/wiki_page_links` with a valid `identifier`/`provider`, or directly via Rails console: `Wikis::RelationPageLink.create!(linkable: work_package, identifier: wiki_page.slug, provider: Wikis::Provider.find_by(universal_identifier: "internal"), author: current_user)`).
    
3.  Call `GET /api/v3/work_packages/{id}/wiki_page_links` for that work package (or the global `GET /api/v3/wiki_page_links` collection).
    

### What is the buggy behavior?

*   The request returns **HTTP 500** with `{"_type":"Error","errorIdentifier":"urn:openproject-org:api:v3:errors:InternalServerError","message":"An internal error has occurred."}`.
    
*   Reproduced directly via Rails console (bypassing the API layer) with the exact same result — confirmed as a genuine backend bug, not a request-formatting or client issue:
    

```text
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  syntax error at or near ")"
LINE 1: ...as title FROM "wiki_page_links" LEFT JOIN (VALUES ) AS metad...
```

*   Root cause, traced through the source (`modules/wikis/app/services/wikis/`):
    
    1.  `adapters/providers/internal/queries/page_info.rb`, `PageInfo#call` resolves a page link&#x27;s `identifier` via `WikiPage.visible(user).find_by(id: input_data.identifier)` — i.e. it treats `identifier` as the wiki page&#x27;s **numeric database id**, not its slug, even though the API representer (`lib/api/v3/page_links/page_link_representer.rb`) documents `identifier` as an opaque, provider-defined string. A caller passing the page&#x27;s real slug (the only value a normal API consumer would have) gets `not_found` from this lookup.
        
    2.  `page_link_metadata_service.rb`, `PageLinkMetadataService#call`/`#enrich_models`: `fetch_metadata` resolves each page link&#x27;s identifier through the adapter above and `filter_map`s away every link whose lookup fails. Because of bug (1), this is effectively _every_ real-world link, so `metadata` ends up empty even when the `page_links` relation itself is non-empty. `enrich_models` then does `Array.new(metadata.size, "(?,?)").join(",")`, which is an empty string when `metadata` is empty, producing the invalid `LEFT JOIN (VALUES ) AS metadata(...)` SQL fragment shown above.
        
    3.  17.7 added an `if relation.any?` guard in `call` (checking whether the `page_links` relation itself is empty) that fixes the trivial zero-links case, but this checks the wrong emptiness — it needed to check whether `metadata` (the post-resolution array) ended up empty, not `relation`. As a result the bug still reproduces on 17.7.1 whenever at least one link exists, regardless of whether its `identifier` is a made-up string or points at a real, existing wiki page.
        

### What is the expected behavior?

1.  `GET .../wiki_page_links` should return `200` with the collection of page links for the work package, each including its resolved title metadata where available.
    
2.  If a given link&#x27;s identifier cannot be resolved by its provider (e.g. the page\_info lookup genuinely fails), that link should still be returned — with its `title` metadata simply absent/null — rather than causing the entire collection request to 500.
    
3.  Separately: `Adapters::Providers::Internal::Queries::PageInfo#call` should resolve `identifier` consistently with what the API representer documents and what `RelationPageLink#identifier` is actually populated with elsewhere (the page&#x27;s slug), not the numeric id.
    

### Logs

Rails console reproduction (no browser/HTTP layer involved):

```text
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  syntax error at or near ")"
LINE 1: ...as title FROM "wiki_page_links" LEFT JOIN (VALUES ) AS metad...
                                                             ^
.../activerecord-8.1.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:169:in 'PG::Connection#exec_params'
.../activerecord-8.1.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:169:in 'ActiveRecord::ConnectionAdapters::PostgreSQL::DatabaseStatements#perform_query'
... (raised from Wikis::PageLinkMetadataService#enrich_models)
```

Via the REST API, the equivalent request returns:

```text
HTTP/1.1 500 Internal Server Error
content-type: application/hal+json; charset=utf-8
{"_type":"Error","errorIdentifier":"urn:openproject-org:api:v3:errors:InternalServerError","message":"An internal error has occurred."}
```

### Screenshots and other files

None — this is a pure backend/API bug with no UI reproduction path tested; the Rails-console and REST-API reproductions above are sufficient to demonstrate it independent of any client.

### Environment information

**OpenProject installation type**

*   Docker All-in-one container (official `openproject/openproject` images: `16.6.10`, `17.6.0`, `17.7.1`)
    

**OpenProject version**

Reproduced on `16.6.10`, `17.6.0`, and `17.7.1` (the latest release in the 17.7 line at the time of testing).

**Browser**

Not applicable — reproduced via direct Rails console and REST API calls, no browser involved.

**Operating System**

Not applicable — server-side bug, reproduced against Linux-based Docker containers; the client OS is irrelevant to the reproduction.

**Language**

Not applicable — no UI/browser interaction involved in the reproduction.
