OpenAPITools / OpenAPITools/openapi-generator

JsonParser#getString() is valid only for KEY_NAME, VALUE_STRING, VALUE_NUMBER parser states. But current parser state is VALUE_NULL

Open
#21,015 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist

The custom Deserializer method not able to handle null value for enum and throwing below exception

JsonParser#getString() is valid only for KEY_NAME, VALUE_STRING, VALUE_NUMBER parser states. But current parser state is VALUE_NULL

Using Open openapi-generator-maven-plugin v7.11.0 with below configuration:

    <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>7.11.0.0</version>
            <configuration>
                <addCompileSourceRoot>true</addCompileSourceRoot>
                <configOptions>
                    <useJakartaEe>true</useJakartaEe>
                    <microprofileRestClientVersion>3.0</microprofileRestClientVersion>
                    <modelNullable>true</modelNullable>
                    <prependFormOrBodyParameters>true</prependFormOrBodyParameters>
                    <serializableModel>false</serializableModel>
                    <dateLibrary>java8</dateLibrary>
                    <java17>true</java17>
                    <useOptionalForEnumFromValue>true</useOptionalForEnumFromValue>
                    <openApiNullable>false</openApiNullable>
                </configOptions>
            </configuration>

Below is the enum definiton in openapi.yaml .

    division:
      type: string
      enum:
        - PC
      example: PC

and its generate enum class as below

@JsonbTypeSerializer(DivisionEnum.Serializer.class)
@JsonbTypeDeserializer(DivisionEnum.Deserializer.class)
public enum DivisionEnum {

PC(String.valueOf("pc"));


String value;

DivisionEnum (String v) {
    value = v;
}

public String value() {
    return value;
}

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

public static Optional<DivisionEnum> fromValue(String v) {
    for (DivisionEnum b : DivisionEnum.values()) {
        if (String.valueOf(b.value).equals(v)) {
            return Optional.of(b);
        }
    }
    return Optional.empty();
}

public static final class Deserializer implements JsonbDeserializer<DivisionEnum> {
    @Override
    public DivisionEnum deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
        for (DivisionEnum b : DivisionEnum.values()) {
            if (String.valueOf(b.value).equals(parser.getString())) {
                return b;
            }
        }
        throw new IllegalArgumentException("Unexpected value '" + parser.getString() + "'");
    }
}

public static final class Serializer implements JsonbSerializer<DivisionEnum> {
    @Override
    public void serialize(DivisionEnum obj, JsonGenerator generator, SerializationContext ctx) {
        generator.write(obj.value);
    }
}

parser.getString() throwing below error when enum value is null.

JsonParser#getString() is valid only for KEY_NAME, VALUE_STRING, VALUE_NUMBER parser states. But current parser state is VALUE_NULL

Note: Using jakarta 10 and java 17

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

Reproduce the failure using the openapi-generator-maven-plugin configuration and nullable enum example in the report. Trace the generated DivisionEnum.Deserializer and the Java generation path that produces it, then add a regression test for a null enum value. Done means deserialization no longer calls JsonParser#getString() while the parser is in VALUE_NULL state.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.