OpenAPITools / OpenAPITools/openapi-generator

[REQ] Export URL path construction functions in typescript-fetch generated client

Open
#22,122 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Is your feature request related to a problem? Please describe.

When using the typescript-fetch generator with Mock Service Worker (MSW) for API mocking, I face a duplication problem. Both the generated API client and MSW mock handlers need to construct the same URL paths, but currently the path construction logic is embedded directly within the API methods and not exposed for external use. ( https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache#L282 )

This leads to:

  • Code duplication: I have to manually reimplement the same URL path patterns in MSW handlers
  • Maintenance burden: When API paths change in the OpenAPI spec, I need to update both the generated client and the mock handlers
  • Contract drift: Risk of inconsistencies between client and mock paths due to manual implementation
  • Reduced type safety: MSW handlers lose the type-safe path construction that the generated client enjoys

Describe the solution you'd like

I'd like the typescript-fetch template to generate and export standalone functions for URL path construction that can be reused by MSW mock handlers.

For example, given an OpenAPI path /pets/{id}, the generator should produce:

// Currently generated
export class PetApi {
  getPetById = (id: string): Promise<Pet> => {
    // path construction inline
  }
}

// Proposed additional exports
export const paths = {
  getPetById: (id: string): string => `/pets/${encodeURIComponent(id)}`
}

Or even better, export a more structured approach:

export const endpointBuilders = {
  pets: {
    getPetById: (params: { id: string }) => `/pets/${encodeURIComponent(params.id)}`,
    listPets: (params?: { limit?: number }) => `/pets${params?.limit ? `?limit=${params.limit}` : ''}`
  }
} as const;

This would allow MSW handlers to reuse the exact same path construction logic:

import { endpoints } from './generated-client';

// In MSW handler
rest.get(endpoints.pets.getPetById({ id: ':id' }), (req, res, ctx) => {
  // Mock implementation
})

Describe alternatives you've considered

  1. Manual path construction: Currently reimplementing paths in MSW handlers, but this breaks when OpenAPI spec changes and requires manual synchronization.

  2. Runtime path extraction: Attempting to intercept and extract URLs from actual requests, but this doesn't work for request matching in MSW setup phase.

  3. Custom template modifications: Maintaining a fork of the template with this functionality, but this adds maintenance overhead and version upgrade complexity.

  4. External path generation tools: Using separate libraries to generate paths, but this creates dependency on additional tools and potential consistency issues.

Additional context

This feature would significantly improve the development experience when using OpenAPI Generator in combination with API mocking solutions like MSW. It supports the growing practice of contract-first API development where the same API specification drives both client implementation and testing infrastructure.

The change should be backward compatible - adding new exports without breaking existing generated client functionality. The path construction functions should handle all OpenAPI path features including:

  • Path parameters with proper encoding
  • Query parameters (optional)
  • Validation against the OpenAPI schema

This approach aligns well with the principles of high cohesion and low coupling, as it separates path construction concern from API client logic, making both the generated client and mocking setup more maintainable.

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 with the typescript-fetch template at modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache#L282, where path construction is currently embedded in API methods. Decide the export shape before implementing it, then ensure generated clients retain existing behavior while exposing reusable builders for encoded path parameters and query parameters. The issue is done when the new exports are backward compatible and cover the listed OpenAPI path features.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.