fix for #37

Closes #37 (closed)

Merge request reports

Loading
+18
Changes for src/main/java/org/etsi/osl/tmf/ri639/model/ResourceRefOrValue.java: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
package org.etsi.osl.tmf.ri639.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import org.springframework.validation.annotation.Validated;

@@ -16,6 +17,23 @@ import jakarta.validation.constraints.NotNull;
@jakarta.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2021-07-08T09:52:18.013684600+03:00[Europe/Athens]")
@Entity(name = "RIResourceRefOrVal")
@JsonIgnoreProperties( {"uuid"} )
/*
 * This is a concrete value type, not a polymorphic base: it is what a ResourceOrderItem carries, and
 * the column it is persisted in is a RIResourceRefOrVal, so no "@type" can ever change the class
 * read here. Resource's own @JsonTypeInfo must therefore not be inherited.
 *
 * If it is, the declared type of the property is ResourceRefOrValue while the inherited
 * defaultImpl is LogicalResource -- a *sibling*, not a subtype. Jackson cannot use a default it
 * cannot assign, so it is left with no fallback and a payload that omits "@type" (or sends it null)
 * fails with "missing type id property '@type'". That is a 400 on every POST /resourceOrder whose
 * order item carries a resource, and it is why reading a ResourceRefOrValue on its own broke too.
 *
 * Id.NONE says what is true rather than papering over it with a defaultImpl of this class: there is
 * no subtype of ResourceRefOrValue to choose between. "@type" is still read and written as the
 * ordinary inherited free-form field, so the wire format is unchanged. Reading a Resource still
 * resolves LogicalResource/PhysicalResource -- see ResourceTypeIdResolver.
 */
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
public class ResourceRefOrValue extends Resource  {

	public void setId(@NotNull String id) {
+6
Changes for src/main/java/org/etsi/osl/tmf/ri639/model/ResourceTypeIdResolver.java: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -35,6 +35,12 @@ import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
 * {@code @type} to their own class name in their constructors, nothing populates {@code @type} for a
 * Resource, so a null value is normal and means "logical" — matching the producer's else-branch and
 * the {@code defaultImpl} declared on {@link Resource}.
 *
 * <p>That {@code defaultImpl} is inherited by every subclass of Resource, and it can only be used for
 * a property whose declared type it is assignable to. {@link ResourceRefOrValue} is a sibling of
 * {@code LogicalResource}, so it opts out with {@code @JsonTypeInfo(use = Id.NONE)} — see the comment
 * there. Any further subclass of Resource used as a declared property type has to do the same, or
 * declare a {@code defaultImpl} of its own.
 */
public class ResourceTypeIdResolver extends TypeIdResolverBase {

+125
Changes for src/test/java/org/etsi/osl/tmf/ri639/model/ResourcePolymorphicTypeTest.java: 125 added lines, 0 removed lines.
Original line number Diff line number Diff line
package org.etsi.osl.tmf.ri639.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.etsi.osl.tmf.ro652.model.ResourceOrderItem;
import org.junit.jupiter.api.Test;

/**
 * The {@code "@type"} polymorphism declared on {@link Resource}.
 *
 * <p>Two things have to hold at once, and getting only the first is what broke resource ordering:
 *
 * <ul>
 * <li>reading a {@code Resource} gives back the concrete subclass the inventory would have picked,
 * and a payload with no {@code "@type"} still reads (it means "logical");
 * <li>reading a property whose declared type is a <b>sibling</b> of that default -- there is exactly
 * one, {@link ResourceOrderItem#getResource()}, declared as {@link ResourceRefOrValue} -- still
 * reads, and reads as the declared type.
 * </ul>
 */
public class ResourcePolymorphicTypeTest {

  private final ObjectMapper mapper = new ObjectMapper();

  /**
   * The regression this test exists for: a resource order posted without {@code "@type"} on its
   * order item resource. Jackson cannot fall back to {@code Resource}'s {@code defaultImpl}
   * ({@code LogicalResource}) here because that class is a sibling of {@code ResourceRefOrValue},
   * not a subtype, so it used to abort with <em>missing type id property '@type' (for POJO property
   * 'resource')</em> -- surfacing as HTTP 400 on POST /resourceOrder.
   */
  @Test
  public void orderItemResourceReadsWithNoAtType() throws Exception {

    String json = "{\"id\":\"xxxx\",\"action\":\"add\",\"resource\":{\"name\":\"aResource\"}}";

    ResourceOrderItem item = mapper.readValue(json, ResourceOrderItem.class);

    assertNotNull(item.getResource());
    assertEquals("aResource", item.getResource().getName());
  }

  /**
   * The order item field is a concrete {@code RIResourceRefOrVal} entity, so whatever
   * {@code "@type"} text the caller sends, the value read into it stays a
   * {@link ResourceRefOrValue}. The text itself is preserved verbatim, exactly as the resource
   * inventory preserves it.
   */
  @Test
  public void orderItemResourceStaysARefOrValueWhateverTheAtTypeSays() throws Exception {

    String json = "{\"id\":\"xxxx\",\"action\":\"add\","
        + "\"resource\":{\"name\":\"aResource\",\"@type\":\"PhysicalResource\"}}";

    ResourceOrderItem item = mapper.readValue(json, ResourceOrderItem.class);

    assertInstanceOf(ResourceRefOrValue.class, item.getResource());
    assertEquals("PhysicalResource", item.getResource().getType());
  }

  /** What commit "fix for #35" was for: a caller reads Resource and gets the concrete subclass. */
  @Test
  public void resourceWithPhysicalAtTypeReadsAsPhysicalResource() throws Exception {

    Resource r = mapper.readValue("{\"name\":\"aResource\",\"@type\":\"PhysicalResource\"}",
        Resource.class);

    assertInstanceOf(PhysicalResource.class, r);
  }

  /** {@code "@type"} on the wire is free-form text, matched case-insensitively as a substring. */
  @Test
  public void resourceWithNonCanonicalPhysicalAtTypeStillReadsAsPhysicalResource()
      throws Exception {

    Resource r = mapper.readValue("{\"name\":\"aResource\",\"@type\":\"urn:x:physicalresource\"}",
        Resource.class);

    assertInstanceOf(PhysicalResource.class, r);
  }

  /** Nothing populates {@code "@type"} for a Resource, so absent means logical. */
  @Test
  public void resourceWithNoAtTypeReadsAsLogicalResource() throws Exception {

    Resource r = mapper.readValue("{\"name\":\"aResource\"}", Resource.class);

    assertInstanceOf(LogicalResource.class, r);
  }

  /**
   * {@code include=EXISTING_PROPERTY}: the inherited {@code "@type"} field writes itself and Jackson
   * adds no id of its own, so a resource serializes exactly as it did before the annotation.
   */
  @Test
  public void serializingAResourceAddsNoTypeIdOfItsOwn() throws Exception {

    Resource r = new Resource();
    r.setName("aResource");

    String json = mapper.writeValueAsString(r);

    assertFalse(json.contains("\"@type\":\"Resource\""), json);
    assertFalse(json.contains("\"@type\":\"LogicalResource\""), json);
  }

  /** A ResourceRefOrValue round-trips on its own, with no type id needed either way. */
  @Test
  public void resourceRefOrValueRoundTrips() throws Exception {

    ResourceRefOrValue value = new ResourceRefOrValue();
    value.setId("res-1");
    value.setName("aResource");

    ResourceRefOrValue readBack =
        mapper.readValue(mapper.writeValueAsString(value), ResourceRefOrValue.class);

    assertEquals("res-1", readBack.getId());
    assertEquals("aResource", readBack.getName());
  }
}
Loading