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

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

Open
KUPRAM-PRAMOD opened this issue Apr 2, 2025 · 0 comments

Comments

@KUPRAM-PRAMOD
Copy link

KUPRAM-PRAMOD commented Apr 2, 2025

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

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

No branches or pull requests

1 participant