Repository navigation
Support Gotenberg 9 alongside 7 and 8 - #95
Conversation
…it calls [GotenbergTest] takes an exclusive 'until' version next to 'since', so a test of a route or field that Gotenberg 9 removed can skip the servers that no longer have it.
Gotenberg 9 answers 400 to a form field a route does not know; 7 and 8 ignore it. - Gotenberg-Output-Filename goes out as a request header, the only place Gotenberg reads it, so SetResultFileName takes effect. A name that is not printable ASCII is rejected before sending. - Screenshot requests no longer send nativePageRanges. - LibreOffice requests send merge and nativePageRanges once. - PDF conversion requests send each PDF once. Refs ChangemakerStudios#83
Plumbing for Gotenberg 9 support; nothing changes on the wire yet. - GotenbergApiVersion (Auto, V8, V9) on the options and the client. Auto reads the version of the service and picks the API of its major release. - A request's route, headers and form parts are produced when the message is built, through GetApiPath(api) and ToHttpContent(api). A custom request that overrides only ToHttpContent() is sent to 7 and 8 as before and refused for 9. - The clients of one AddGotenbergSharpClient() registration share a version cache per service address, refreshed from the Gotenberg-Version response header. - GotenbergVersionNotSupportedException can report a feature a newer release removed. - Snapshot tests pin what every request type sends today. Refs ChangemakerStudios#83
Gotenberg 9 renamed nativePageRanges, generateDocumentOutline and several LibreOffice fields, and takes the native watermark color as #rrggbb and its rotation in degrees. [Gotenberg9Field] declares the new name next to the property, and facets render for an API through ToHttpContent(GotenbergApiVersion). Requests to Gotenberg 7 and 8 are unchanged. Chromium conversions and screenshots without post-processing options now pass on Gotenberg 9. Refs ChangemakerStudios#83
Gotenberg 9 replaced the flat post-processing fields with one 'stages' field, a JSON list it runs in the order given. On 9 the client now builds that list from the existing options, in the order Gotenberg 8 applied them, with metadata before the PDF/A and PDF/UA remediation and encryption last: merge (LibreOffice), split or select, stamp-text, rotate, flatten-form, set-metadata, remediate, encrypt. generateTaggedPdf becomes nativeTaggedPdf. Options without a faithful equivalent on 9 throw GotenbergVersionNotSupportedException before anything is sent: watermarks, stamps with free-form options or an image or PDF source, metadata keys outside the eight fields 9 writes, and PDF/A with a password. SupportsAsync and EnsureSupportedAsync answer for these as well. Requests to Gotenberg 7 and 8 are unchanged. Refs ChangemakerStudios#83
Gotenberg 9 replaced forms/pdfengines/* with forms/pdf/*. On 9 every write operation (merge, convert, flatten, rotate, split, encrypt, embed, write metadata, write bookmarks) goes to forms/pdf/pipe as a stage at its place in the stage order, together with the request's other options; reading metadata and bookmarks goes to forms/pdf/metadata and forms/pdf/bookmarks. - Embedded files are uploaded under 'files' with one attach stage each. - Bookmarks are written with pageNumber and read from page or pageNumber. - Writing bookmarks per file has no equivalent on 9 and throws GotenbergVersionNotSupportedException before anything is sent. - A PDF conversion without PDF/A or PDF/UA is refused before sending. Requests to Gotenberg 7 and 8 are unchanged. The whole suite now passes against Gotenberg 7, 8 and 9. Refs ChangemakerStudios#83
Gotenberg 8 takes the file of an image or PDF watermark or stamp from the form field 'watermark' or 'stamp', and since 8.32.0 answers 400 without one. The client had no way to send it, so those sources could not work. - SetImageWatermark, SetPdfWatermark, SetImageStamp and SetPdfStamp carry the file and send it in that field to Gotenberg 7 and 8 (works from 8.28.0, the release that added watermarks and stamps). - On Gotenberg 9 an image or PDF stamp becomes a stamp-image or stamp-pdf stage that names the file uploaded under 'files'. Watermarks still have no equivalent on 9.
Gotenberg 9 requires Gotenberg-Webhook-Events-Url with the webhook URL and no longer reads the error URL and error method: errors arrive as webhook.error events. - WebhookBuilder.SetEventsUrl. A webhook needs an error URL or an events URL. - Gotenberg 7 and 8 get the headers they always did, plus the events URL when set (8.29.0 added it). - Gotenberg 9 gets the URL, the events URL, the method and the extra headers. A webhook without an events URL is refused before anything is sent. Refs ChangemakerStudios#83
WithStages on the Chromium and merge builders, and PdfEngineBuilders.Pipe for forms/pdf/pipe, send an explicit stage list: typed methods for merge, split, select, rotate, the three stamps with position, size and opacity, flatten-form, set-metadata, set-bookmarks, attach, remediate, encrypt and optimize, and Add for every other operation of Gotenberg 9. Explicit stages are sent as listed and do not combine with the options the client maps to stages. They need Gotenberg 9: against 7 and 8 the request is refused before anything is sent. Refs ChangemakerStudios#83
Five examples still used the namespaces of before 3.0 and did not compile; they are not in the solution, so nothing built them. The webhook example sets the events URL that Gotenberg 9 requires, and the watermark example stamps through WithStages when the service is Gotenberg 9, where watermarks have no equivalent. Refs ChangemakerStudios#83
- The integration tests start Gotenberg 7, 8 and 9, each with a longer API timeout so that a cold Chromium start does not fail the first conversion. - A docs page lists what the client translates for Gotenberg 9 and what differs there; the quick starts and the compose file pin gotenberg/gotenberg:9. - The release notes of the unreleased section read as one note. Closes ChangemakerStudios#83
Six of the fifteen example projects were not in GotenbergSharpApiClient.sln, so neither dotnet build nor CI compiled them, and five had stopped compiling unnoticed. They are packed no more than before: the examples set IsPackable to false.
|
Java4shell vulnerability is a thing now which is flagging gotenberg 8 in our vulnerability scanner. If there is something we can do to speed up this PR to get gotenberg 9 support? |
Oh that sounds VERY urgent. You should report this to the proper folks who can fix that issue here: https://github.com/gotenberg/gotenberg |
There was a problem hiding this comment.
Thanks for this. Supporting 7, 8 and 9 in one package turned out nicer than I expected. I'd like to merge it with two changes:
-
Failed
/versionlookups. Caching a successful lookup once per address is fine. When/versionfails with a 401, 403 or 5xx, though, the conversion fails with a generic error and the next request tries again. Please throw a clear error there instead, one that tells the user to setApiVersion = V8orV9. That covers the case where a proxy blocks/version. -
Non-ASCII
ResultFileName. Sending it as the header is the right fix, since the form field never did anything. Throwing on non-ASCII means names that used to do nothing quietly now throw on 7 and 8 too. Please clean the name up instead: strip accents (Müller→Muller) and replace any other non-printable ASCII, line breaks included, with_.
With those two in, 7 and 8 users see no breaking change.
A few smaller things came up in review, like SupportsAsync and rendering disagreeing on stamps without a file, and the Rotate angle check. They can come in a follow-up after the merge.
A 401, 403 or 5xx answer to GET /version surfaced as the bare error of a route the caller never called. It is now a GotenbergApiException that says what Gotenberg answered, why the client reads the version, and to set ApiVersion to V8 or V9, or EnforceMinimumVersion to false when ApiVersion is already set; a 401 also points at the credentials. It keeps the status code and the original as InnerException, so existing catch blocks still match. The failure is still not cached. With ApiVersion = V9 the routes with an 8.x minimum release no longer read the version: a Gotenberg 9 service has them all.
Gotenberg-Output-Filename is a header, which cannot carry characters outside printable ASCII. Throwing for such a name made requests fail on Gotenberg 7 and 8 that used to send it to no effect. The name is now sent with its accents stripped (Müller as Muller) and every other such character, line breaks included, replaced by '_'. ResultFileName keeps the name as it was set.
An image or PDF stamp set with SetSource and an expression naming a file on the server threw ArgumentException when rendered for Gotenberg 9, while SupportsAsync answered true. Gotenberg 9 has no way to read a file named on the server, so the stamp is now refused like the other features it removed: GotenbergVersionNotSupportedException before anything is sent, naming SetImageStamp and SetPdfStamp, and SupportsAsync answers false. Gotenberg 7 and 8 are unchanged.
StagesBuilder.Rotate accepted any multiple of 90, 0 and 450 included, while SetRotationOptions and PdfEngineBuilders.Rotate accept 90, 180 or 270 through RotationAngle, the rule of Gotenberg 8. Rotate now goes through RotationAngle too, with an overload that takes one. StagesBuilder is not released yet.
|
Important Review skippedToo many files! This PR contains 152 files, which is 52 over the limit of 100. To get a review, reduce the PR to 100 files or fewer by splitting it into smaller PRs or changing its base branch. Upgrade to a paid plan to raise the limit. CodeRabbit couldn't verify your organization's usage billing state, so no usage-priced review was started. Retry after the billing service recovers.
|
|
Thanks for the review. Both changes are in, and so are the two smaller points, since they were small:
The review mentions a few smaller things "like" these two. If there are others, list them and I'll add them here or in a follow-up, whichever you prefer. The suite passes against Gotenberg 7, 8 and 9 (1116 tests, 0 failed), and I updated the PR description and |
The shared version cache replaced its version only when a response reported another major release. A service upgraded in place from 8.20.0 to 8.30.0 kept a gated route such as rotate refused until the application restarted, and a rollback sent requests that answered with a bare 404. The cache now takes the version of any response that differs from the cached one, an unknown cached version included.
…ework
A result file name with a noncharacter (U+FFFF, U+FFFE, U+FDD0) or an
unassigned code point threw ArgumentException ("Invalid Unicode code
point found") on .NET Framework, which refuses to normalize them; .NET 5
and later returned them unchanged. Such characters now skip the
normalization and give "_" on every runtime, and an ArgumentException
from Normalize does the same.
…0 to Gotenberg 8 wrote the relationship of an embedded file as given, so "data" or any string worked. The attach stage of Gotenberg 9 takes only Source, Data, Alternative, Supplement and Unspecified and compares them case-sensitively. On 9 Embed now sends one of the five in another case in its canonical form and refuses any other value before sending, naming the five; StagesBuilder.Attach accepts the five in any case the same way. Requests to 7 and 8 are unchanged.
SupportsAsync said true for a request that then threw InvalidOperationException when sent to Gotenberg 9: a class that overrides only ToHttpContent(), or a PDF operation with no stage to run (WriteMetadata with every value null), which Gotenberg 9 answers 400 "stages is required". One check, BuildRequestBase.FindUnrenderableOn, now decides both; rendering throws what it returns, and SupportsAsync and EnsureSupportedAsync answer with it. Every request for the pipe route refuses an empty list with a message naming what is missing. Requests to 7 and 8 are unchanged.
A URL request with AddExtraResources answered 400 on Gotenberg 9, whose URL route refuses form fields it does not know. Gotenberg 8.40 and 9.5 read neither field, so Gotenberg 8 ignored them. They are no longer sent to 9, which converts the page as 8 did; 7 and 8 still receive them. The docs of AddExtraResources and the Gotenberg 9 table say so.
A stamp image "café.png" or an embedded "données.xml" was answered 400
("form file 'café.png' is required"): .NET writes a file name outside
ASCII as an RFC 2047 encoded word, which Go's multipart reader keeps as
text, so the stage could not find its file. Files that a stage reads now
go as filename* (RFC 5987), which Go decodes. Requests to 7 and 8 and
ASCII names are written as before. The check that refuses a stage file
named like another upload sees such names too.
…ions The docs said Gotenberg 8 read the post-processing options on the merge and split routes only. In 8.40 the merge route reads every one of them except the split options, the split route reads them all, the conversion route reads PDF/A and PDF/UA, and the other PDF routes read none. On 9 the client applies them to every PDF operation, so a merge with split options returns a zip there and one PDF on 8. The release note and the Gotenberg 9 table now state that.
|
While waiting I went through the library code once more and found a few more things, now fixed in seven commits on top (the commit table in the description has them):
Apart from the file-name fix, the requests sent to Gotenberg 7 and 8 are unchanged; their wire snapshots pass as they were. The suite passes against 7, 8 and 9: 1145 tests, 0 failed. |
Brings in ChangemakerStudios#95 (Gotenberg 9 support). Its stage code still used Newtonsoft.Json, so this merge also ports it: StageList writes the stages with JsonObjectWriter, StageMetadata reads metadata from the dictionary types, and StagesBuilder.Add takes an IDictionary<string, object> or a JsonObject instead of an anonymous object or a JObject. Bookmark reads the pageNumber of Gotenberg 9 with System.Text.Json, and LegacyContentOverrides keeps its reflection trim-safe. The tests parse JSON with System.Text.Json.Nodes, and the wire snapshots are updated for compact JSON.
Closes #83.
One package now serves Gotenberg 7, 8 and 9. The client reads the version of the service once and sends each request with the routes and form fields of that major release, so the same builder code works on all three. Where Gotenberg 9 has no equivalent, the caller gets a
GotenbergVersionNotSupportedExceptionbefore anything is sent, naming what to use instead.Against 9 the released client fails on almost every call: Gotenberg 9 answers 400 to a form field it does not know, and the client always sends
generateDocumentOutline. The PDF engine routes answer 404.The design questions in #83 are still open. I went ahead with the route I leaned towards there (one package, dispatch on the version) so that there is something concrete to look at. If you would rather go another way, or want this split into several PRs, say so.
How to review
It is large: 23 commits and about 11,700 added lines, of which 6,600 are tests and snapshots and 4,600 are the library. Each commit is one step, builds on its own, and was run against the Gotenberg containers before the next one, so commit by commit is the easy way through. The first two stand alone as fixes for Gotenberg 8; I can send them separately if you want them first.
[GotenbergTest]gets an exclusiveuntilversion, for tests of what a newer Gotenberg droppedApiVersion, rendering a request when it is sent, a shared version cache, and snapshot tests of what every request type sends today[Gotenberg9Field]stageslist of Gotenberg 9forms/pdf/pipe, and the read routes toforms/pdf/metadataandforms/pdf/bookmarks. From here the suite passes on 7, 8 and 9WithStagesandPdfEngineBuilders.Pipe, to list stages directlyGET /versionfails, andApiVersion = V9skips the lookup of the 8.x floorsSupportsAsyncagreesStagesBuilder.Rotatetakes 90, 180 or 270, like the other rotate methodsdataasData), any other is refused before sendingSupportsAsyncanswers false for a request that cannot be rendered for 9: a legacy custom class, or a PDF request with no stageextraLinkTagsandextraScriptTagsare not sent to Gotenberg 9, which refuses them; neither 8 nor 9 reads themcafé.png, is found when its name is not ASCIITwo new docs pages carry the detail:
docs/gotenberg-9.md(how the API is picked, what is translated, a table of what differs on 9 and what to do) anddocs/stages.md. The "Unreleased" section ofCHANGES.MDis the release note.What changes for a caller on Gotenberg 7 and 8
Requests are unchanged, apart from the fixes of the second commit; snapshot tests written against the old code pin that. A caller notices:
GET /versionper service address.ApiVersionisAutoby default, so every application now reads the version once, not only those that use a version-gated route. When it fails with anything but a 404, the request throws aGotenbergApiExceptionthat says what Gotenberg answered and to setApiVersiontoV8orV9, orEnforceMinimumVersion = falsewhenApiVersionis set already; it keeps the status code, so existingcatchblocks still match.ApiVersion = V8orV9skips the lookup, and withV9the gated routes skip it too. The clients of oneAddGotenbergSharpClient()registration share the cached version, and theGotenberg-Versionresponse header refreshes it when the service is upgraded.SetResultFileNametakes effect. The name went out as a form field, which Gotenberg never read; it is now theGotenberg-Output-Filenamerequest header. A name outside printable ASCII is cleaned up before it is sent: accents are stripped (Mülleris sent asMuller) and other characters become_.GotenbergVersionNotSupportedException.RequirementandRequiredVersionare nullable: they are null when the exception reports a removed feature (RemovedInVersion,Replacement).ToHttpContent()is sent to 7 and 8 exactly as before, and is refused for 9 with a message naming the method to override.On Gotenberg 9
Translated for you: renamed fields, post-processing options as stages in the order Gotenberg 8 applied them, PDF operations through
forms/pdf/pipe, bookmarks (pageNumber), embedded files asattachstages, text, image and PDF stamps.Refused before sending, with the replacement named: watermarks (9 only draws over the page), stamps with free-form options, image or PDF stamps set without their file, metadata keys outside the eight fields 9 writes, bookmarks written per file, PDF/A together with a password, and a webhook without an events URL.
SupportsAsyncandEnsureSupportedAsyncanswer for these too.New API:
WithStages(s => …)on the HTML, URL and merge builders andPdfEngineBuilders.Pipe(s => …), with typed stage methods andAdd(op, options)for every other operation;SetEventsUrl;SetImageStamp,SetPdfStamp,SetImageWatermarkandSetPdfWatermark.Everything about Gotenberg 9 comes from its source at 9.5.1 and 8.40.0 and from running against the containers. Two things the source showed that I did not expect: a split by pages wrote one PDF per page on 8, where Gotenberg's upgrade guide says one per range, so it maps to
selectfollowed bysplitwithevery: 1; and the Chromium routes of 8 never readflatten, so a flatten on an HTML or URL request does nothing on 8 and is applied on 9.Where I would like your view
forms/pdf/pipeand I apply them all, so anEncryptPdfRequestwithRotationOptionsgets a rotation on 9 and none on 8, and aMergeRequestwithSplitOptionsreturns a zip of PDFs on 9 and one PDF on 8. Dropping them on 9 to match 8 is a small change.SetPositionon a stamp needs Gotenberg 9.4.0 and the client does not check it; 9.0 to 9.3 answer 400.StageList, so that Add Native AOT support and replace Newtonsoft.Json with System.Text.Json #82 has one place to change.Tests
dotnet testwith no version override now starts Gotenberg 7, 8 and 9 containers: 1145 tests, 1064 passed, 81 skipped by version, 0 failed. Before the first commit the suite had 22 failures on 9 and none on 7 and 8.deploy.ymlis unchanged: it runsdotnet testonce and the harness starts the three containers. They now run with--api-timeout=5m, because the first Chromium conversion on a freshly started Gotenberg 7 once answered 503 after its default 30 seconds.dotnet build -c Releasebuilds the library for its five target frameworks, the tests and all fifteen examples;dotnet packstill produces only the library package.Left for the release
I did not touch
<PackageReleaseNotes>, the release line of the README or the "Unreleased" heading ofCHANGES.MD, since the version number is yours to choose.