OpenAPITools / OpenAPITools/openapi-generator

[BUG] Generated code omitting model type details for composed string schemas

Open
#10,862 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

Description

In the attached openapi, I define two schemas, schema date with type: string and format: date, and schema
timestamp with type: string and format: date-time. I then create two new schemas composed
from these and add readOnly: true to these. (This is to define a type for immutable timestamps, such
as createdAt, modifiedAt, and similar properties.)

openapi-generator does not generate definitions (types/interfaces/classes) for date and timestamp
and thus the generated code does not compile.

openapi-generator version

4.3.0-SNAPSHOT

OpenAPI declaration file content or url

See openapi-generator-date-models.yaml

Generation Details
openapi-generator generate -g typescript-axios -i openapi.yaml -o gen/sdk/typescript-axios

The interfaces for readOnlyDate and readOnlyTimestamp are disconnected from their composition
and are thus incorrect. Here is an excerpt from the emitted api.ts

export interface Dates {
    'date'?: string;
    'roDate'?: ReadOnlyDate;
    'timestamp'?: string;
    'roTimestamp'?: ReadOnlyTimestamp;
}

/**
 * A readonly or derived date, formatted in [RFC 3339](https://tools.ietf.org/html/rfc3339) `date` format: `YYYY-MM-DD`.
 * @export
 * @interface ReadOnlyDate
 */
export interface ReadOnlyDate {
}
/**
 * A readonly or derived timestamp (an instant in time) formatted in [RFC 3339](https://tools.ietf.org/html/rfc3339) `date-time` UTC format: `YYYY-MM-DDThh:mm:ss.sssZ`.
 * @export
 * @interface ReadOnlyTimestamp
 */
export interface ReadOnlyTimestamp {
}

Nothing constraint the properties roDate and toTimestamp to string as is done for date and timestamp.

A similar issue occurs when using the java generator. There is no model class
generated for the schemas date or timestamp (using private LocalDate date and private OffsetDateTime timestamp). Instead, the model
src/main/java/org/openapitools/client/model/ReadOnlyDate.java is defined with:

/**
 * A readonly or derived date, formatted in [RFC 3339](https://tools.ietf.org/html/rfc3339) `date` format: `YYYY-MM-DD`.
 */
@ApiModel(description = "A readonly or derived date, formatted in [RFC 3339](https://tools.ietf.org/html/rfc3339) `date` format: `YYYY-MM-DD`.")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2021-11-15T11:16:39.501242-05:00[America/New_York]")
public class ReadOnlyDate {

It is disconnected from the LocalDate class used for the date properties.

ReadOnlyTimestamp.java has

@ApiModel(description = "A readonly or derived timestamp (an instant in time) formatted in [RFC 3339](https://tools.ietf.org/html/rfc3339) `date-time` UTC format: `YYYY-MM-DDThh:mm:ss.sssZ`.")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2021-11-15T11:22:30.796419-05:00[America/New_York]")
public class ReadOnlyTimestamp {
}

and is similarly unrelated to the OffsetDateTime used for the timestamp property/schema.

Steps to reproduce

Run the CLI above

Related issues/PRs
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-generator-date-models.yaml and reproduce the issue using the shown typescript-axios CLI command. Compare the generated ReadOnlyDate and ReadOnlyTimestamp models with the date and timestamp properties, then repeat with the Java generator. Done means composed string schemas retain the underlying date and date-time model types in both generated outputs and compile correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, typescript
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.