swagger-api / swagger-api/swagger-codegen-generators

[Java] Enum String scheme generates invalid enum class

Open
#996 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

openapi: 3.0.1
info:
  version: 1.0.0
  title: Test
servers:
  - url: /
tags:
  - name: test
paths:
  /repository:
    get:
      tags:
        - test
      operationId: test
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/requestStatus'
components:
  schemas:
    requestStatus:
      type: string
      enum: [ a,b,c ]

The above specification, generates the following enum RequestStatus class:

/*
 * Test
 * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
 *
 * OpenAPI spec version: 1.0.0
 * 
 *
 * 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.model9.appextract.openapi.models;

import java.util.Objects;
import java.util.Arrays;
import com.google.gson.annotations.SerializedName;
import javax.validation.constraints.*;
import javax.validation.Valid;
import java.io.IOException;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

/**
 * Gets or Sets requestStatus
 */
@JsonAdapter(RequestStatus.Adapter.class)
public enum RequestStatus {
  A("a"),
  B("b"),
  C("c");

  private String value;

  RequestStatus(String value) {
    this.value = value;
  }

  public String getValue() {
    return value;
  }

  @Override
  public String toString() {
    return String.valueOf(value);
  }

  public static RequestStatus fromValue(String input) {
    for (RequestStatus b : RequestStatus.values()) {
      if (b.value.equals(input)) {
        return b;
      }
    }
    return null;
  }

  public static class Adapter extends TypeAdapter<RequestStatus> {
    @Override
    public void write(final JsonWriter jsonWriter, final RequestStatus enumeration) throws IOException {
      jsonWriter.value(String.valueOf(enumeration.getValue()));
    }

    @Override
    public RequestStatus read(final JsonReader jsonReader) throws IOException {
      Object value = jsonReader.nextString();
      return RequestStatus.fromValue(()(value));
    }
  }
}

Note the last return line in the class:
return RequestStatus.fromValue(()(value));

I'm using the org.hidetake.swagger.generator gradle plugin and io.swagger.codegen.v3:swagger-codegen-cli:3.0.30

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 provided OpenAPI specification and inspect how the Java enum RequestStatus and its Gson Adapter are generated. Reproduce the output with swagger-codegen-cli 3.0.30 and the org.hidetake.swagger.generator Gradle plugin; done means the generated enum compiles and its adapter returns the parsed value correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
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.