swagger-api / swagger-api/swagger-codegen

[Java] Client Code Generation: Wrong Timestamp import

Open
#10,440 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have a definition of Timestamp object in yaml file. But, when I am generating client for Java, 'java.sql.Timestamp' class is getting imported in models instead of user defined Timestamp.

Swagger-codegen version

swagger 2.0

Swagger example code

JSON Example:

{
   "swagger": "2.0",
   "info": {
      "title": "RequestHeader Example Definition",
      "description": "Example to describe swagger codegen issue",
      "version": "v1"
   },
   "schemes": [
      "http",
      "https"
   ],
   "paths": {
      "/v1/requestHeader": {
         "post": {
            "tags": [
               "request_header"
            ],
            "operationId": "Authenticate",
            "description": "Test Request Header",
            "parameters": [
               {
                  "name": "body",
                  "description": "The request body.",
                  "in": "body",
                  "schema": {
                     "$ref": "#/definitions/RequestHeader"
                  }
               }
            ],
            "responses": {
               "default": {
                  "description": "Successful operation",
                  "schema": {
                     "$ref": "#/definitions/RequestHeader"
                  }
               }
            }
         }
      }
   },
   "definitions": {
      "Timestamp": {
         "description": "A timestamp object representing a point on the ISO timeline in milliseconds\nsince the Unix epoch.",
         "type": "object",
         "properties": {
            "epochMillis": {
               "description": "Milliseconds since the Unix epoch",
               "type": "string",
               "format": "int64"
            }
         }
      },
      "RequestHeader": {
         "description": "Header object that is defined on all requests sent to the server.",
         "type": "object",
         "properties": {
            "requestTimestamp": {
               "description": "**REQUIRED**: Timestamp of this request. The receiver must verify that\nthis timestamp is ± 60s of 'now', and reject the request if it is not. This\nrequest timestamp is not idempotent upon retries.",
               "$ref": "#/definitions/Timestamp"
            }
         }
      }
   }
}

YAML Example:

  swagger: '2.0'
info:
  title: RequestHeader Example Definition
  description: Example to describe swagger codegen issue
  version: v1
schemes:
  - http
  - https
paths:
  /v1/requestHeader:
    post:
      tags:
        - request_header
      operationId: Authenticate
      description: |-
        Test Request Header
      parameters:
        - name: body
          description: The request body.
          in: body
          schema:
            $ref: '#/definitions/RequestHeader'
      responses:
        default:
          description: Successful operation
          schema:
            $ref: '#/definitions/RequestHeader'
definitions:
  Timestamp:
    description: |-
      A timestamp object representing a point on the ISO timeline in milliseconds
      since the Unix epoch.
    type: object
    properties:
      epochMillis:
        description: Milliseconds since the Unix epoch
        type: string
        format: int64
  RequestHeader:
    description: Header object that is defined on all requests sent to the server.
    type: object
    properties:
      requestTimestamp:
        description: |-
          **REQUIRED**: Timestamp of this request. The receiver must verify that
          this timestamp is ± 60s of 'now', and reject the request if it is not. This
          request timestamp is not idempotent upon retries.
        $ref: '#/definitions/Timestamp'

Generates RequestHeader model as below:

/*
 * RequestHeader Example Definition
 * Example to describe swagger codegen issue
 *
 * OpenAPI spec version: v1
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */


package io.swagger.client.model;

import java.util.Objects;
import java.util.Arrays;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.IOException;
import java.sql.Timestamp;

/**
 * Header object that is defined on all requests sent to the server.
 */
@ApiModel(description = "Header object that is defined on all requests sent to the server.")
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2020-08-25T00:40:42.550Z")



public class RequestHeader {
  @SerializedName("requestTimestamp")
  private Timestamp requestTimestamp = null;

  public RequestHeader requestTimestamp(Timestamp requestTimestamp) {
    this.requestTimestamp = requestTimestamp;
    return this;
  }
Steps to reproduce
  1. Copy above JSON or YAML example to Swagger Editor
  2. Generate Client for Java
  3. In generated Request Header model 'java.sql.Timestamp' class imported instead of user defined Timestamp class

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 Java client generation path and the type/import mapping used for the provided Swagger 2.0 definition. Reproduce generation from the JSON or YAML example in Swagger Editor, then inspect the generated RequestHeader model. Done means the user-defined Timestamp model is referenced without the incorrect java.sql.Timestamp import.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.