OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] Client generated oneOf model broken if data-type is array

Open
#17,745 4 comments 3 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
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Object array in oneof item generate wrong data structure, build error illegal start of expression

openapi-generator version

openapi-generator-cli-7.2.0.jar

OpenAPI declaration file content or url

oneOfArrayMapImport.yaml

Generation Details

Command line used for generation

openapi-generator-cli generate \
  -g java --library native \
  -i name_of_file.yaml \
  -o myApi
Steps to reproduce

Issues in the generated client:

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
@JsonDeserialize(using = Fruit.FruitDeserializer.class)
@JsonSerialize(using = Fruit.FruitSerializer.class)
public class Fruit extends AbstractOpenApiSchema {
    ...
        @Override
        public Fruit deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            JsonNode tree = jp.readValueAsTree();
            Object deserialized = null;
            boolean typeCoercion = ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS);
            int match = 0;
            JsonToken token = tree.traverse(jp.getCodec()).nextToken();
            // deserialize List<Grape>
            try {
                boolean attemptParsing = true;
                // ensure that we respect type coercion as set on the client ObjectMapper
                if (List<Grape>.class.equals(Integer.class) || List<Grape>.class.equals(Long.class) || List<Grape>.class.equals(Float.class) || List<Grape>.class.equals(Double.class) || List<Grape>.class.equals(Boolean.class) || List<Grape>.class.equals(String.class)) {
                    attemptParsing = typeCoercion;
                    if (!attemptParsing) {
                        attemptParsing |= ((List<Grape>.class.equals(Integer.class) || List<Grape>.class.equals(Long.class)) && token == JsonToken.VALUE_NUMBER_INT);
                        attemptParsing |= ((List<Grape>.class.equals(Float.class) || List<Grape>.class.equals(Double.class)) && token == JsonToken.VALUE_NUMBER_FLOAT);
                        attemptParsing |= (List<Grape>.class.equals(Boolean.class) && (token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE));
                        attemptParsing |= (List<Grape>.class.equals(String.class) && token == JsonToken.VALUE_STRING);
                    }
                }
                if (attemptParsing) {
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(List<Grape>.class);
                    // TODO: there is no validation against JSON schema constraints
                    // (min, max, enum, pattern...), this does not perform a strict JSON
                    // validation, which means the 'match' count may be higher than it should be.
                    match++;
                    log.log(Level.FINER, "Input data matches schema 'List<Grape>'");
                }
            } catch (Exception e) {
                // deserialization failed, continue
                log.log(Level.FINER, "Input data does not match schema 'List<Grape>'", e);
            }

            // deserialize Map<String, Apple>
            try {
                boolean attemptParsing = true;
                // ensure that we respect type coercion as set on the client ObjectMapper
                if (Map<String, Apple>.class.equals(Integer.class) || Map<String, Apple>.class.equals(Long.class) || Map<String, Apple>.class.equals(Float.class) || Map<String, Apple>.class.equals(Double.class) || Map<String, Apple>.class.equals(Boolean.class) || Map<String, Apple>.class.equals(String.class)) {
                    attemptParsing = typeCoercion;
                    if (!attemptParsing) {
                        attemptParsing |= ((Map<String, Apple>.class.equals(Integer.class) || Map<String, Apple>.class.equals(Long.class)) && token == JsonToken.VALUE_NUMBER_INT);
                        attemptParsing |= ((Map<String, Apple>.class.equals(Float.class) || Map<String, Apple>.class.equals(Double.class)) && token == JsonToken.VALUE_NUMBER_FLOAT);
                        attemptParsing |= (Map<String, Apple>.class.equals(Boolean.class) && (token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE));
                        attemptParsing |= (Map<String, Apple>.class.equals(String.class) && token == JsonToken.VALUE_STRING);
                    }
                }
                if (attemptParsing) {
                    deserialized = tree.traverse(jp.getCodec()).readValueAs(Map<String, Apple>.class);
                    // TODO: there is no validation against JSON schema constraints
                    // (min, max, enum, pattern...), this does not perform a strict JSON
                    // validation, which means the 'match' count may be higher than it should be.
                    match++;
                    log.log(Level.FINER, "Input data matches schema 'Map<String, Apple>'");
                }
            } catch (Exception e) {
                // deserialization failed, continue
                log.log(Level.FINER, "Input data does not match schema 'Map<String, Apple>'", e);
            }

    ...

}
Related issues/PRs
Suggest a fix

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 modules/openapi-generator/src/test/resources/3_0/oneOfArrayMapImport.yaml and generate the Java client with the native library using the command shown. Trace the generated Fruit deserializer for the List and Map<String, Apple> oneOf branches, then verify that the generated client compiles and handles the array data type without illegal Java syntax.

Written by the indexing model from the issue text.

Assessment

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