OpenAPITools / OpenAPITools/openapi-generator

[BUG] typescript-angular loses schema composition and generates interfaces but should use types for string schemas

Open
#12,185 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report Checklist
Description

Generated code for generators typescript-angular, java, go (perhaps others) do not honor schema composition for primitive (non-object) schemas.

Some related issues: #11701 #10125

openapi-generator version

5.4.0

OpenAPI declaration file content or url

openapi.yaml gist

Generation Details
openapi-generator-cli generate -g typescript-angular -i openapi.yaml -o sdk/typescript-axios
openapi-generator-cli generate -g java	         -i openapi.yaml -o sdk/java
openapi-generator-cli generate -g go  	         -i openapi.yaml -o sdk/go
Steps to reproduce

see above

Related issues/PRs

typescript-angular - generates a type for ReadOnlyResourceId but not for ResourceId; ReadOnlyResourceId does not
use/reference ResourceId or string type: sdk/typescript-axios/model/readOnlyTimestamp.ts :

export interface ReadOnlyTimestamp { 
}

Same is true for ReadOnlyTimestamp in sdk/typescript-axios/model/readOnlyTimestamp.ts

export interface ReadOnlyTimestamp { 
}

Since this is not an object type, I think codegen should use a Typescript type of string here instead.

// in readOnlyResourceId.ts :
type ReadOnlyResourceId = string;
// in readOnlyTimestamp.ts:
type ReadOnlyTimestamp = string;

Since the schema is readOnly: true, the resource type should also use the readonly keyword
on the properties:

// in resource.ts

import { ReadOnlyResourceId } from './readOnlyResourceId';
import { ReadOnlyTimestamp } from './readOnlyTimestamp';

export interface Resource { 
    readonly id?: ReadOnlyResourceId;
    readonly createdAt?: ReadOnlyTimestamp;
}

Related: In a more complicated API (which I cannot include here), the generated code includes an extends but still does not
generate resourceId.ts:

import { ResourceId } from './resourceId';

export interface ReadOnlyResourceId extends ResourceId { 
}

This won't compile because ResourceId is not defined - there is no './resourceId' file.
But even if it did, a Typescript interface is not correct since interfaces describe object structures, not primitives like string.

java - similar problems with this generator. It generates
sdk/java/src/main/java/org/openapitools/client/model/ReadOnlyResourceId.java and sdk/java/src/main/java/org/openapitools/client/model/ReadOnlyTimestamp.java and
but not sdk/java/src/main/java/org/openapitools/client/model/ResourceId.java
sdk/java/src/main/java/org/openapitools/client/model/Timestamp.java

and it generates a class for type, but really this is not an object type but a String so there should not be a class.

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2022-04-20T13:26:31.680249-04:00[America/New_York]")
public class ReadOnlyResourceId {
 ...
}

Instead the model should use String type

public class Resource {
  @SerializedName(SERIALIZED_NAME_ID)
  private String id;

  public static final String SERIALIZED_NAME_CREATED_AT = "createdAt";
  @SerializedName(SERIALIZED_NAME_CREATED_AT)
  private String createdAt;

  public String getId() {
    return id;
  }

  public Resource id(String id) {
    this.id = id;
    return this;
  }

and so on.

Suggest a fix

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 linked openapi.yaml gist and reproduce the issue using the listed typescript-angular, java, and go generation commands. Compare the generated ResourceId, ReadOnlyResourceId, Timestamp, and ReadOnlyTimestamp model files in the shown SDK paths; done means primitive schema composition is preserved, generated types use the appropriate primitive representation, and the output compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, go, java, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.