palantir / palantir/conjure-java

A union type even in invalid format should deserialize to Unknown

Open
#564 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
39
Forks
49
Avg merge
8h 22m
Merged PRs (30d)
32

Description

What happened?

Given:

    objects:
      Foo:
        fields:
          foo: string
      Bar:
        fields:
          bar: string
      FooBar:
        union:
          foo: Foo
          bar: Bar

the following fails:

        ObjectMapper mapper = ObjectMappers.createDefaultJsonObjectMapper(false);
        String good = "{\n"
                + "  \"type\" : \"bar\",\n"
                + "  \"bar\" : {\n"
                + "    \"bar\" : \"123\"\n"
                + "  }\n"
                + "}";
        String bad = "{\n"
                + "  \"type\" : \"new\",\n"
                + "  \"some-string\""
                + "}";

        assertThat(mapper.readValue(good, FooBar.class))
                .extracting("value")
                .extracting("value")
                .isInstanceOf(Bar.class);
        assertThat(mapper.readValue(bad, FooBar.class))
                .extracting("value")
                .extracting(Object::getClass)
                .extracting(Class::getSimpleName)
                .asString()
                .contains("UnknownWrapper");

What did you want to happen?

The test should pass.

We can achieve this by modifying the generated code for the unknown wrapper to look something like this:

image

this is obviously not completely correct since if type is missing or there exists no sub-object, we'd be setting the raw invalid json as type, so we'd probably have to do a little more work (like making type optional or nullable for the unknown wrapper)

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 generated FooBar union and ObjectMappers.createDefaultJsonObjectMapper(false), then reproduce the good and bad JSON cases from the issue. Trace how the unknown wrapper is selected during deserialization; done means the bad payload yields UnknownWrapper while valid bar input still yields Bar, with coverage for missing type or sub-object behavior.

Written by the indexing model from the issue text.

Assessment

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