Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERRORUNKNOWN in java okhttp4-gson template #12305

Closed
kgrosvenor opened this issue Dec 7, 2023 · 3 comments
Closed

ERRORUNKNOWN in java okhttp4-gson template #12305

kgrosvenor opened this issue Dec 7, 2023 · 3 comments
Assignees

Comments

@kgrosvenor
Copy link

kgrosvenor commented Dec 7, 2023

Description

When generating Java okhttp4-gjson on the latest swagger-codegen-cli.jar models have ERRORUNKNOWN in them where there should be Object

With regards to my swagger json the additionalProperties is set to false, so i am unsure if the JSON could be the issue here.

public class SomethingResponseResponseModel extends HashMap<String, ERRORUNKNOWN> <--------  {

I found a previous issue saying that a later version fixes it, but i tried it and it doesn't seem to!

The reason is this is an issue is because the client generated library is not buildable with ERRORUNKNOWN keyword
#9379

Swagger-codegen version
Swagger declaration file content or url
Command line used for generation

java -jar ..\swagger-codegen-cli.jar generate -i ..\Downloads\combinedSchema.json -l java --library okhttp4-gson

Steps to reproduce

Try to generate an API with swagger-codegen-cli.jar for java with the okhttp4-library

Related issues/PRs

#9379
#9336

Suggest a fix/enhancement

Please recommend the best version to use for okhttp4-gson to work on java

Kind regards

@HugoMario
Copy link
Contributor

Hello @kgrosvenor
is it possible that you can share the OAS definition used to reproduce the issue?

@radarsh
Copy link

radarsh commented Jan 5, 2024

@HugoMario you can use this Swagger 2 spec. Same issue.

swagger: '2.0'
info:
  title: Pets
  version: '1.0'

basePath: /v1
schemes:
  - https
  
paths:
  /pets/{id}:
    get:
      summary: Get pet
      operationId: getPet
      produces:
        - application/json
      parameters:
        - name: id
          in: path
          description: The pet ID
          required: true
          type: string
          format: uuid
      responses:
        200:
          description: The details of the requested pet
          schema:
            $ref: '#/definitions/Pet'
definitions:
  Pet:
    type: object
    properties:
      detail:
        type: object
        additionalProperties: true
        description: Map of strings to objects for the details 

Just paste this in https://editor.swagger.io/, click Generate Client > Java. Go to the downloaded files and look for Pet.java and you will find this which won't compile:

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 io.swagger.client.model.ERRORUNKNOWN;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Pet
 */
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2024-01-05T12:59:44.497Z")
public class Pet {
  @SerializedName("detail")
  private Map<String, ERRORUNKNOWN> detail = null;

  public Pet detail(Map<String, ERRORUNKNOWN> detail) {
    this.detail = detail;
    return this;
  }

  public Pet putDetailItem(String key, ERRORUNKNOWN detailItem) {
    if (this.detail == null) {
      this.detail = new HashMap<String, ERRORUNKNOWN>();
    }
    this.detail.put(key, detailItem);
    return this;
  }

   /**
   * Map of strings to objects for the details
   * @return detail
  **/
  @ApiModelProperty(value = "Map of strings to objects for the details")
  public Map<String, ERRORUNKNOWN> getDetail() {
    return detail;
  }

  public void setDetail(Map<String, ERRORUNKNOWN> detail) {
    this.detail = detail;
  }


  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Pet pet = (Pet) o;
    return Objects.equals(this.detail, pet.detail);
  }

  @Override
  public int hashCode() {
    return Objects.hash(detail);
  }


  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Pet {\n");
    
    sb.append("    detail: ").append(toIndentedString(detail)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(java.lang.Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }

}

@HugoMario HugoMario self-assigned this Jan 23, 2024
@HugoMario
Copy link
Contributor

fixed by #12327

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants