swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Java Client] Types with format are not correctly mapped

Open hiveship opened this issue 10 years ago • 10 comments

Considering the following Swagger Specification:

swagger: '2.0'
info:
  title: Fake API
  description: A simple sample
  version: 1.0.0
host: localhost:8080
schemes:
  - http
basePath: /api
produces:
  - application/json
  - application/xml
securityDefinitions:
  authentication:
    type: basic
    description: HTTP Basic Authentication. Works over `HTTP` 
paths:
  /tenants:
    get:
      security:
          - authentication: []
      summary: Tenants
      description: 
        Get all the existing tenants
      responses:
        '200':
          description: An array of tenants
          schema:
            type: array
            items:
              $ref: '#/definitions/Tenant'
        default:
          description: Unexpected error
          schema:
            $ref: '#/definitions/Error'
definitions:
  Tenant:
    required: [tenant_id]
    type: object
    properties:
      tenant_id:
        type: string
        description: 'Unique identifier representing a specific tenant'
      tenant_uuid:
        type: string
        format: uuid
        description: 'Unique identifier representing a specific tenant'
      display_name:
        type: string
        description: Display name of tenant.
  Error:
    type: object
    properties:
      identifier:
        type: string
        format: uuid
      message:
        type: string
      fields:
        type: string

My model object "Tenant" as an identifier "uuid". I have defined it as a "string" type with the format "uuid". In the Java Client, I was expecting that the field "tenant_uuid" will be an instance of java.util.uuid However, it is just mapped as a string. The "format" element is ignored by the Java code (not even used for documentation).

  /**
   * Unique identifier representing a specific tenant
   **/
  @ApiModelProperty(value = "Unique identifier representing a specific tenant")
  @JsonProperty("tenant_uuid")
  public String getTenantUuid() {
    return tenantUuid;
  }
  public void setTenantUuid(String tenantUuid) {
    this.tenantUuid = tenantUuid;
  }

After a read of the Swagger Specification, I know the formats such as "uuid" are not defined by the specification. However, is it possible to edit the Swagger Codegen for the Java Client to handle the "uuid" format ?

hiveship avatar Jan 04 '16 16:01 hiveship

Yes, you can simply add a check for UUIDProperty in the DefaultCodegen class, under the public String getSwaggerType(Property p) method. Feel free to send a PR with some tests and we can get it merged.

fehguy avatar Jan 04 '16 16:01 fehguy

Ok, I will try it asap

hiveship avatar Jan 05 '16 08:01 hiveship

Can be closed, PR merged https://github.com/swagger-api/swagger-codegen/pull/1899

hiveship avatar Jan 24 '16 14:01 hiveship

I am still experiencing this issue with swagger-codegen version 2.2.1 when I have "type" : "string", "format" : "uuid" I am still getting the type as String in the generated Java client

wilkystorm avatar Oct 17 '16 20:10 wilkystorm

when I go here https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java I do see this at lines 1090-1091: } else if ( p instanceof UUIDProperty) { datatype = "UUID"; I wonder why when I did npm install swagger-codegen on mac I am not getting a java.util.UUID when I run swagger-codegen on my json file which defines the "type" : "string", "format" : "uuid" ? I am downloading the JSON file right from the swagger-editor ....

wilkystorm avatar Oct 17 '16 21:10 wilkystorm

@wilkystorm the issue was fixed in the latest master. Please pull the latest to build the JAR locally and give it another try.

wing328 avatar Oct 18 '16 03:10 wing328

I'm using the latest master and still see the exact same issue. My model has a property with type: string, format: uuid and the resulting type in the generated code is String. I used the both the Java generator and the JaxRS-CXF generator.

Has anyone got it to work as expected?

Specifically, looks like AbstractJavaCodegen.java +123 is the problem. Why does this type mapping exist? When I remove that line the generated code generates java.util.UUID as expected.

MatanRubin avatar Feb 07 '17 11:02 MatanRubin

@MatanRubin please help submit a PR with the suggested fix so that we can review it more easily.

wing328 avatar Feb 07 '17 11:02 wing328

Will submit a PR shortly. BTW, as a workaround people can use type-mappings to avoid this issue:

<type-mappings>UUID=UUID</type-mappings>

MatanRubin avatar Feb 07 '17 11:02 MatanRubin

I'm still seeing this using maven swagger codegen 3.0.0.rc1. For example: parameters: - name: "token" in: "path" required: true type: "string" format: "uuid" still seems to generate a String. Am I missing something?

Edit: It looks like it works for properties, but not path parameters.
Is that a different area of the code?

mhaefele avatar Dec 20 '18 15:12 mhaefele