Skip to content

KTOR-9295: Add decodeFromUrl method#5454

Open
hfhbd wants to merge 3 commits intoktorio:mainfrom
hfhbd:9295-decodeFromUrl
Open

KTOR-9295: Add decodeFromUrl method#5454
hfhbd wants to merge 3 commits intoktorio:mainfrom
hfhbd:9295-decodeFromUrl

Conversation

@hfhbd
Copy link
Copy Markdown
Contributor

@hfhbd hfhbd commented Mar 18, 2026

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 299c462e-780d-4c80-b2b0-0b776b93b83a

📥 Commits

Reviewing files that changed from the base of the PR and between a489126 and 18da9a9.

📒 Files selected for processing (4)
  • ktor-shared/ktor-resources/api/ktor-resources.api
  • ktor-shared/ktor-resources/api/ktor-resources.klib.api
  • ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/ParametersSerializationTest.kt
  • ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/UrlDeserializationTest.kt
🚧 Files skipped from review as they are similar to previous changes (1)
  • ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/UrlDeserializationTest.kt

📝 Walkthrough

Walkthrough

Adds URL-to-resource decoding via a new public decodeFromUrl on ResourcesFormat, exposes collection size in ListLikeDecoder by overriding decodeCollectionSize, and adds comprehensive tests for URL deserialization and updated test resource classes.

Changes

Cohort / File(s) Summary
URL decode implementation
ktor-shared/ktor-resources/common/src/io/ktor/resources/serialization/ResourcesFormat.kt
New public decodeFromUrl(deserializer: KSerializer<T>, url: Url): T that builds Parameters from URL path segments and query parameters and delegates to decodeFromParameters.
Decoder behavior
ktor-shared/ktor-resources/common/src/io/ktor/resources/serialization/Decoders.kt
ListLikeDecoder now overrides decodeCollectionSize(descriptor: SerialDescriptor): Int to return precomputed elementsCount.
Tests — models & updates
ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/ParametersSerializationTest.kt, .../PathPatternSerializationTest.kt
Added/updated resource data classes (e.g., nested User, explicit path/child fields, changed resource parameter name) to reflect path/query decoding semantics.
Tests — URL deserialization
ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/UrlDeserializationTest.kt
New comprehensive test suite exercising decodeFromUrl for simple and nested paths, slash variants, multiple-parent errors, required/optional query params, and trailing list parameters.
Public API signatures
ktor-shared/ktor-resources/api/ktor-resources.api, ktor-shared/ktor-resources/api/ktor-resources.klib.api
Public API surface extended with decodeFromUrl(KSerializer, Url) in ResourcesFormat.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

👍 ship!

Suggested reviewers

  • e5l
  • bjhham
  • marychatte
🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.76% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The description is incomplete. While it correctly identifies the subsystem and references the ticket, the Solution section only states 'Implemented...' without explaining what was actually implemented or how the decodeFromUrl method works. Complete the Solution section by describing the implementation: explain that decodeFromUrl reconstructs Parameters from URL path segments and query parameters, how it handles nested resources and different parameter types (required, optional, variadic), and provide context on the supporting changes to ListLikeDecoder.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately reflects the main change: adding a decodeFromUrl method to the ResourcesFormat class, which is the primary feature introduced across all modified files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (1)
ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/UrlDeserializationTest.kt (1)

20-21: Rename the new tests to the repo's backtick style.

The new test cases still use identifier-style function names. Please switch them to descriptive backtick names so the file matches the rest of the test convention.

As per coding guidelines, "**/test/**/*.kt: Use descriptive test names in backticks following the pattern: describe what is being tested."

Also applies to: 35-36, 57-58, 98-99, 126-127, 134-135, 147-148

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/UrlDeserializationTest.kt`
around lines 20 - 21, Rename the identifier-style test functions in
UrlDeserializationTest.kt (e.g., fun testSimplePath(), and the other test
functions listed around the commented ranges) to descriptive backtick-style
names following the repo pattern (e.g., fun `describe what is being tested`()) —
update each function declaration (for the functions at the marked ranges) to use
a quoted backtick name that describes the behavior under test instead of the
camelCase identifier.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@ktor-shared/ktor-resources/common/src/io/ktor/resources/serialization/ResourcesFormat.kt`:
- Around line 125-167: decodeFromUrl() currently walks resource path parts in
reverse and doesn’t validate literal segments against url.segments, and its
reverse-counting (uses resourcesStart/consumedSegments and
endsWithOptionalParameterName/endsWithTrailingParameterName) mis-aligns parent
placeholders when optional ({name?}) is omitted or tail ({name...}) expands; to
fix, rework the loop in ResourcesFormat.decodeFromUrl to parse
encodeToPathPattern(deserializer) left-to-right (same approach as
UrlBuilder.href()), validating each literal segment against segments at the
current read index, consuming exactly one segment for literals and
single-parameter parts, zero-or-one for optional ({name?}) only when present,
and N segments for trailing ({name...}), updating the read index accordingly and
populating parametersBuilder; remove reliance on reverse resourcesStart
arithmetic and ensure parents are traversed in order (use
current.elementDescriptors -> membersWithAnnotations) so offsets remain correct.
- Around line 114-172: New public API method decodeFromUrl has been added to
ResourcesFormat but the module ABI signature files were not updated; regenerate
the ABI files by running the Gradle task that refreshes legacy ABI signatures
(./gradlew :ktor-shared:ktor-resources:updateLegacyAbi), verify that the
generated ktor-resources.api and ktor-resources.klib.api include the new
decodeFromUrl entry, and commit the updated files to the repo before merging.

In
`@ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/PathPatternSerializationTest.kt`:
- Around line 37-48: The nested resource inner classes reference the wrong
parent type so encodeToPathPattern() no longer traverses the parent variants;
update the inner data class constructors so ChildClassWithSlash and
ChildClassWithoutSlash inside NestedClassWithSlash use parent:
NestedClassWithSlash, and the ChildClassWithSlash inside NestedClassWithoutSlash
uses parent: NestedClassWithoutSlash (i.e., restore the parent parameter types
to the enclosing NestedClass variant) so the parent trailing-slash cases are
exercised again.

In
`@ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/UrlDeserializationTest.kt`:
- Line 10: Remove the inaccessible import of
io.ktor.tests.resources.ParametersSerializationTest.V1 at the top of the file;
it conflicts with and is unnecessary because this test file defines its own V1
(see the V1 declaration around line 112). Delete the import statement
referencing ParametersSerializationTest.V1 so the local V1 is used and the
private symbol is not referenced.

---

Nitpick comments:
In
`@ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/UrlDeserializationTest.kt`:
- Around line 20-21: Rename the identifier-style test functions in
UrlDeserializationTest.kt (e.g., fun testSimplePath(), and the other test
functions listed around the commented ranges) to descriptive backtick-style
names following the repo pattern (e.g., fun `describe what is being tested`()) —
update each function declaration (for the functions at the marked ranges) to use
a quoted backtick name that describes the behavior under test instead of the
camelCase identifier.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8df9aff5-f851-493e-8450-c9d4c8e2c1d2

📥 Commits

Reviewing files that changed from the base of the PR and between 630e201 and a489126.

📒 Files selected for processing (5)
  • ktor-shared/ktor-resources/common/src/io/ktor/resources/serialization/Decoders.kt
  • ktor-shared/ktor-resources/common/src/io/ktor/resources/serialization/ResourcesFormat.kt
  • ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/ParametersSerializationTest.kt
  • ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/PathPatternSerializationTest.kt
  • ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/UrlDeserializationTest.kt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant