Skip to content

Error generating adapter for interface whose children have no fields #410

@joaonmatos

Description

@joaonmatos

Version: 3.7-RC2

Reproduce:

import io.avaje.jsonb.Json.SubType;

@io.avaje.jsonb.Json
@SubType(type = Csv.class, name = "csv")
@SubType(type = Json.class, name = "json")
@SubType(type = Xml.class, name = "xml")
public sealed interface Exporter permits Csv, Json, Xml {}

public record Csv() implements Exporter {}

public record Json() implements Exporter {}

public record Xml() implements Exporter {}

Generated code:

import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
import io.avaje.json.JsonWriter;
import io.avaje.json.PropertyNames;
import io.avaje.jsonb.AdapterFactory;
import io.avaje.jsonb.Jsonb;
import io.avaje.jsonb.Types;
import io.avaje.jsonb.spi.Generated;
import java.io.IOException;

@Generated("io.avaje.jsonb.generator")
public final class ExporterJsonAdapter implements JsonAdapter<Exporter>{

  // naming convention Match

  private final JsonAdapter<String> stringJsonAdapter;
  private final PropertyNames names;

  public ExporterJsonAdapter(Jsonb jsonb) {
    this.stringJsonAdapter = jsonb.adapter(String.class);
    this.names = jsonb.properties("@type", );
  }

  @Override
  public void toJson(JsonWriter writer, Exporter exporter) {
    writer.beginObject(names);
    if (exporter instanceof final Csv sub) {
      writer.name(0);
      stringJsonAdapter.toJson(writer, "csv");
    }
    else if (exporter instanceof final Json sub) {
      writer.name(0);
      stringJsonAdapter.toJson(writer, "json");
    }
    else if (exporter instanceof final Xml sub) {
      writer.name(0);
      stringJsonAdapter.toJson(writer, "xml");
    }
    writer.endObject();
  }

  @Override
  public Exporter fromJson(JsonReader reader) {
    // variables to read json values into, constructor params don't need _set$ flags

    String type = null;

    // read json
    reader.beginObject(names);
    while (reader.hasNextField()) {
      final String fieldName = reader.nextField();
      switch (fieldName) {
        case "@type":
          type = stringJsonAdapter.fromJson(reader);
          break;
        default:
          reader.unmappedField(fieldName);
          reader.skipValue();
      }
    }
    reader.endObject();

    if (type == null) {
      throw new IllegalStateException("Missing Required @type property that determines deserialization type");
    }
    return switch (type) {
      case "csv" -> {
        Csv _$csv = new Csv();
        yield _$csv;
      }
      case "json" -> {
        Json _$json = new Json();
        yield _$json;
      }
      case "xml" -> {
        Xml _$xml = new Xml();
        yield _$xml;
      }
      default -> {
        throw new IllegalStateException("Unknown value for @type property " + type);
        }
    };
  }
}

Errors while compiling with:

ExporterJsonAdapter.java:23: error: illegal start of expression
    this.names = jsonb.properties("@type", );

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions