Skip to content

Tag based grouping for controllers#488

Open
DevNamedZed wants to merge 2 commits intofabrikt-io:masterfrom
DevNamedZed:implement_tag_grouping
Open

Tag based grouping for controllers#488
DevNamedZed wants to merge 2 commits intofabrikt-io:masterfrom
DevNamedZed:implement_tag_grouping

Conversation

@DevNamedZed
Copy link

@DevNamedZed DevNamedZed commented Jan 1, 2026

Add option that enables grouping controllers by the first tag instead of URL path segments.

  • Add tag based grouping for controllers
    • Add new controller generation option - TAG_GROUPING
    • Update Ktor, Micronaut, Spring controller generators
    • Add unit tests and test in playground

Copy link
Collaborator

@ulrikandersen ulrikandersen left a comment

Choose a reason for hiding this comment

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

Nice addition 💯

enum class ControllerCodeGenOptionType(val description: String) {
SUSPEND_MODIFIER("This option adds the suspend modifier to the generated controller functions"),
AUTHENTICATION("This option adds the authentication parameter to the generated controller functions"),
TAG_GROUPING("This option groups controllers based on the first tag rather than paths"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we make it even more clear what the option does?

Suggested change
TAG_GROUPING("This option groups controllers based on the first tag rather than paths"),
GROUP_BY_TAG("This option groups controllers based on the first tag rather than paths"),

* @param tagGrouping if true, groups paths by their first operation tag;
* if false, groups by the first URI path segment (default behavior)
*/
fun OpenApi3.routeToPaths(tagGrouping: Boolean): Map<String, Map<String, Path>> =
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would suggest a more explicit strategy approach as this API is a little vague.

Something along the lines of:

enum class GroupingStrategy {
    BY_FIRST_TAG,
    BY_FIRST_PATH_SEGMENT
}

fun OpenApi3.groupedPaths(groupingStrategy: GroupingStrategy)

Perhaps with an even more clear return:

typealias GroupKey = String
typealias Route = String
typealias GroupedPaths = Map<GroupKey, Map<Route, Path>>

fun OpenApi3.groupedPaths(groupingStrategy: GroupingStrategy): GroupedPaths

Copy link
Author

Choose a reason for hiding this comment

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

Sorry for late reply. I updated per comments. Thanks for library and taking this change

@ulrikandersen ulrikandersen self-assigned this Jan 6, 2026
- Add new controller generation option - TAG_GROUPING
- Update Ktor, Micronaut, Spring controller generators
- Add unit tests and test in playground
- Use enum instead of boolean
- Use type aliases instead of primitive types and collections
- Rename functions for clearer meaning
@DevNamedZed DevNamedZed force-pushed the implement_tag_grouping branch from 3287bd7 to e99f030 Compare January 20, 2026 06:00

typealias GroupKey = String
typealias Route = String
typealias GroupedPaths = Map<GroupKey, Map<Route, Path>>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know this is a popular paradigm amoug some, but I personally dislike it. Please revert these typealias definitions, they give me post-scala ptsd :)


private fun Path.firstOperationTagOrNull(): String? {
val opsInStableOrder = listOfNotNull(
get, post, put, patch, delete, options, head, trace
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we avoid hardcoding this? We have QUERY nearly in spec and I don't want to change this

}

@Test
fun `ensure TAG_GROUPING produces controllers grouped by first tag`() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

rather than add a dedicated test, either pick one of the resource/examples api specs and add tag grouping, or follow the existing pattern and commit generated code examples.

The pattern we use is that the test directory forms a living documentation of what is generated

}

@Test
fun `ensure TAG_GROUPING produces controllers grouped by first tag`() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment as pervious. Follow existing test setup with expected output committed as files

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.

3 participants