Swagger-codegen: [Java Client] Types with format are not correctly mapped

Created on 4 Jan 2016  路  10Comments  路  Source: swagger-api/swagger-codegen

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 ?

Java Bug

All 10 comments

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.

Ok, I will try it asap

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

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 the issue was fixed in the latest master. Please pull the latest to build the JAR locally and give it another try.

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 please help submit a PR with the suggested fix so that we can review it more easily.

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

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

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?

Was this page helpful?
0 / 5 - 0 ratings