OpenAPITools / OpenAPITools/openapi-generator

[REQ] option to customize ObjectSerializer in typescript-generator

Open
#16,427 0 comments 0 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.

the ObjectSerializer of the typescript-generator does not allow customization. e.g. it's hard to add application/xml media-type support. we ended up with something like this:

/**
 * The {@link ObjectSerializer} does not support the
 * media-type `application/xml` by default. We have to re-implement some of its
 * methods in such a way that it does.
 *
 * @see https://www.w3.org/XML/2012/10/3023bis/draft-ietf-appsawg-xml-mediatypes-08_diff
 */
export const addXmlSupportToSerializer = (objectSerializer: ObjectSerializer | any): void => {
    const parseOriginal = objectSerializer.parse;
    objectSerializer.parse = (rawData: string, mediaType: string | undefined): string | KeyValues => {
        return parseOriginal(rawData, mediaType === MediaType.XML ? MediaType.TEXT : mediaType);
    };

    const getPreferredMediaTypeOriginal = objectSerializer.getPreferredMediaType;
    objectSerializer.getPreferredMediaType = (mediaTypes: Array<string>): string => {
        const normalMediaTypes: (string | undefined)[] = mediaTypes.map(objectSerializer.normalizeMediaType);
        return normalMediaTypes.includes(MediaType.XML) ? MediaType.XML : getPreferredMediaTypeOriginal(mediaTypes);
    };

    const stringifyOriginal = objectSerializer.stringify;
    objectSerializer.stringify = (data: any, mediaType: string): string => {
        return stringifyOriginal(data, mediaType === MediaType.XML ? MediaType.TEXT : mediaType);
    };
};¨

as far as I am considered, one should not (under no circumstances) override static methods in TypeScript. this is mostly due to the unknown global impact in an applicaiton. please correct me, if I am wrong.

Describe the solution you'd like

It would be cool to be able to customize the ObjectSerializer without the need to override its static methods.

Describe alternatives you've considered

Tried reaching into the class, but failed because of the static methods. Ended up overriding these. I think that

  1. either the ObjectSerializer should allow customization (e.g. no static methods, or additional methods to register custom serializers)
  2. or the ObjectSerializer should be an interface/base-class and should be optionally passable to the underlying *ApiRequestFactory extends BaseAPIRequestFactory: myApi.myFunction(parameters, { serializer: myCustomSerializer }).

Additional context

Using the Maven plugin for generation: https://mvnrepository.com/artifact/org.openapitools/openapi-generator-maven-plugin/6.6.0.

                        <configuration>
                            <configOptions>
                                <oas3>true</oas3>
                                <useTags>true</useTags>
                            </configOptions>
                            <generatorName>typescript</generatorName>
                        </configuration>

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 by inspecting the generated TypeScript ObjectSerializer and the *ApiRequestFactory/BaseAPIRequestFactory entry points mentioned in the issue. Determine how serializer customization could be exposed without overriding static methods, then verify that generated API clients can use a custom serializer while retaining existing behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.