OpenAPITools / OpenAPITools/openapi-generator

Sorting operations by operationId leads to issues on generated server

Open
#14,660 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Suppose we have the following routes (in form operationName=route path):

getTopScoredPosts=/posts/top-scored
getPostById=/posts/{postId}

Operations are sorted by operationId like below:

ops.sort((one, another) -> ObjectUtils.compare(one.operationId, another.operationId));

https://github.com/OpenAPITools/openapi-generator/blob/1b60ee1799a6831a9cd59cb833db63d4ee4eb06b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java#L604

When generating server, routes are going to be registered with a router (like express) in the following order:

getPostById=/posts/{postId}
getTopScoredPosts=/posts/top-scored
```

This leads to an issue where requests like `GET /posts/top-scored` will be actually matched by `GET /posts/{postId}` route handler.

The simplest solution is to avoid sorting operations and have them in order as they are defined in the opeanpi doc.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java around the operation sorting at line 604, then trace how generated server routes receive that order. Verify the behavior with the documented /posts/top-scored and /posts/{postId} routes; done means the specific route is matched correctly instead of being captured by the parameterized route.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.