swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

1.5.x: Definition of descendant classes using ApiModel.subTypes is not working

Open unoexperto opened this issue 11 years ago • 11 comments

Definition of descendant classes is not reflected in the documentation. I'm using Swagger 1.5.2-M1.

Example of my base class

@ApiModel(description = "Basic class for timesheet entry", subTypes = {TimesheetProjectEntryWire.class, TimesheetAdminEntryWire.class}, discriminator = "type")
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonTypeIdResolver(TimesheetEntryTypeIdResolver.class)
public abstract class TimesheetEntryWireBase {}

one of the descendant classes is

@ApiModel(description = "Timesheet project entry object", parent = TimesheetEntryWireBase.class)
public class TimesheetProjectEntryWire extends TimesheetEntryWireBase {}

resulting documentation doesn't have descendant classes. Here is output json

"TimesheetEntryWireBase": {
      "required": [
        "entryDate",
        "entryHours",
        "id",
        "isBillable",
        "notes",
        "type"
      ],
      "properties": {
        "type": {
          "type": "string",
          "description": "entry type",
          "enum": [
            "PROJECT",
            "ADMINISTRATIVE",
            "UNKNOWN"
          ]
        },
        "id": {
          "type": "integer",
          "format": "int64",
          "description": "timesheet id"
        },
        "entryHours": {
          "type": "number",
          "format": "double",
          "description": "number of hours spent"
        },
        "isBillable": {
          "type": "boolean",
          "description": "are hours billable?"
        },
        "notes": {
          "type": "string",
          "description": "text notes"
        },
        "entryDate": {
          "type": "string",
          "description": "entry date"
        }
      },
      "description": "Basic class for timesheet entry",
      "discriminator": "type"
    }

What am I missing ?

unoexperto avatar Mar 10 '15 16:03 unoexperto

This is expected, he subtype support is expected in M2, out shortly

fehguy avatar Mar 10 '15 16:03 fehguy

Great! What's the ETA roughly ?

unoexperto avatar Mar 10 '15 16:03 unoexperto

We are targeting 3/14 but it's possibly a week out.

fehguy avatar Mar 10 '15 16:03 fehguy

Duplicate of #847.

webron avatar Mar 10 '15 17:03 webron

I don't know if you guys had any updates on this but as of 3 days ago this bug was still present in develop_2.0 branch.

unoexperto avatar Apr 29 '15 10:04 unoexperto

Reopening for now pending further investigation. I believe the behavior here has changed from 1.3.

webron avatar Apr 29 '15 10:04 webron

I've just tested this. All works well.

Class structure:

@ApiModel(description = "Basic class for timesheet entry", subTypes = {TimesheetProjectEntryWire.class, TimesheetAdminEntryWire.class}, discriminator = "type")
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonTypeIdResolver(TimesheetEntryTypeIdResolver.class)
public abstract class TimesheetEntryWireBase {
}

@ApiModel(description = "Timesheet project entry object", parent = TimesheetEntryWireBase.class)
abstract public class TimesheetEntryTypeIdResolver extends TimesheetEntryWireBase implements TypeIdResolver {
}

@ApiModel(description = "Timesheet project entry object", parent = TimesheetEntryWireBase.class)
public class TimesheetAdminEntryWire extends TimesheetEntryWireBase {
}

@ApiModel(description = "Timesheet project entry object", parent = TimesheetEntryWireBase.class)
public class TimesheetProjectEntryWire extends TimesheetEntryWireBase {
}

JSON-output:

{
  "TimesheetAdminEntryWire" : {
    "allOf" : [ {
      "$ref" : "#/definitions/TimesheetEntryWireBase"
    }, {
      "type" : "object",
      "description" : "Timesheet project entry object"
    } ]
  },
  "TimesheetEntryWireBase" : {
    "type" : "object",
    "discriminator" : "type",
    "description" : "Basic class for timesheet entry"
  },
  "TimesheetProjectEntryWire" : {
    "allOf" : [ {
      "$ref" : "#/definitions/TimesheetEntryWireBase"
    }, {
      "type" : "object",
      "description" : "Timesheet project entry object"
    } ]
  }
}

MDoumin avatar May 28 '15 15:05 MDoumin

@cppexpert - closing this now as tested by @MDoumin. Please reopen if still an issue.

webron avatar Jun 05 '15 21:06 webron

Hey, I'm trying to generate a python REST client based on Swagger documentation, using a swagger.json generated by swagger-maven-plugin-3.1.0 I have 2 DTOs that extend an abstract base class:

@ApiModel(subTypes = {FileSourceDTO.class, MemorySourceDTO.class}, discriminator = "classType")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "classType")
@JsonSubTypes({
        @JsonSubTypes.Type(value = FileSourceDTO.class, name = "FileSourceDTO"),
        @JsonSubTypes.Type(value = MemorySourceDTO.class, name = "MemorySourceDTO") })
public abstract class LogSourceDTO implements BaseIdentifiableDTO<UUID>  {..}
@ApiModel(description = "FileSourceDTO", parent = LogSourceDTO.class)
publi` class FileSourceDTO extends LogSourceDTO {..}
@ApiModel(description = "MemorySourceDTO", parent = LogSourceDTO.class)
public class MemorySourceDTO extends LogSourceDTO {..}

In the resulting REST client, the FileSourceDTO and MemorySourceDTO classes don't contain a 'classType' field (which should be in the REST request as it is generated and expected by jackson) even though it should.

In addition, when I called (using the generated python REST client) an API method that returns an object of one of the children classes, an object of the abstract LogSourceDTO class was actually created instread (losing all the additional fields the children class adds over it).

I tried adding the above @ApiModel annotations after reading the comments here, but it doesn't seem to solve this. Am I doing something wrong?

Many, many thanks.

itayavra avatar Nov 23 '15 16:11 itayavra

@itayavra - if you're using the swagger-magen-plugin, you'd need to open an issue on that project.

webron avatar Nov 24 '15 00:11 webron

Hey, I'm trying to generate a python REST client based on Swagger documentation, using a swagger.json generated by swagger-maven-plugin-3.1.0 I have 2 DTOs that extend an abstract base class:

@ApiModel(subTypes = {FileSourceDTO.class, MemorySourceDTO.class}, discriminator = "classType")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "classType")
@JsonSubTypes({
        @JsonSubTypes.Type(value = FileSourceDTO.class, name = "FileSourceDTO"),
        @JsonSubTypes.Type(value = MemorySourceDTO.class, name = "MemorySourceDTO") })
public abstract class LogSourceDTO implements BaseIdentifiableDTO<UUID>  {..}
@ApiModel(description = "FileSourceDTO", parent = LogSourceDTO.class)
publi` class FileSourceDTO extends LogSourceDTO {..}
@ApiModel(description = "MemorySourceDTO", parent = LogSourceDTO.class)
public class MemorySourceDTO extends LogSourceDTO {..}

In the resulting REST client, the FileSourceDTO and MemorySourceDTO classes don't contain a 'classType' field (which should be in the REST request as it is generated and expected by jackson) even though it should.

In addition, when I called (using the generated python REST client) an API method that returns an object of one of the children classes, an object of the abstract LogSourceDTO class was actually created instread (losing all the additional fields the children class adds over it).

I tried adding the above @apimodel annotations after reading the comments here, butble it doesn't seem to solve this. Am I doing something wrong?

Many, many thanks.

Are you able to get any work around it?

shankar96 avatar Feb 21 '20 07:02 shankar96