简体   繁体   中英

Jackson mapper. Could not resolve subtype of [simple type, class ]: missing type id property '' (for POJO property '')

I am using openapi generator in order to generate my classes

Base class

@JsonPropertyOrder({
  EntityType.CONTAINER
})
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "...")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "structure_type", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = SonClass1, name = "sonClass1"),
  @JsonSubTypes.Type(value = SonClass2, name = "sonClass2"),
})
public class MyBaseClass {
  public static final String JSON_PROPERTY_CONTAINER = "container";
  private String container;
}

SonClass1 example

@JsonPropertyOrder({
  NodeType.JSON_PROPERTY_PROPERTIES,
})
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "...")
public class SonClass1 extends MyBaseClass {
  public static final String JSON_PROPERTY_PROPERTIES = "properties";
  private Map<String, Property> properties = null;
}

I am trying to build object using ObjectMapper using mapper.readValue(); Result is exception:

com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.MySon1]: missing type id property 'structure_type' 

Could you please explain me why is that, and what I could do with this? Does it mean I have to add property "structure_type" on each subclass?

Openapi yaml look like this

MyBaseClass:
  type: object
  properties:
    container:
      type: string
  discriminator:
    propertyName: structure_type
    mapping:
      sonClass: '#/sonClass1'


sonClass1:
  allOf:
  - $ref: '#/MyBaseClass'
  - type: object
    properties:
      properties:
        type: object
        additionalProperties:
          $ref: '#/Property'

My base class should have “structure_type” property, after adding this - everything is working.

In my case, json string was not matched with the JAVA object type. I corrected my json string,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM