0
votes

I'm researching OpenAPI spec to be used in a C# project. My API uses generic types, like

    class Message<T>
    {
        public string Id { get; set; }
        public T Payload { get; set; }
    }

    class BasicClientState
    {
        public int Count { get; set; }
        public double FaultPercentage { get; set; }
    }

like this

    class SomeApiController
    {
        public Message<DateTime> GetLatestRebootMessage() 
        {
            throw new NotImplementedException();
        }
        public Message<BasicClientState> GetLatestBasicClientStateMessage()
        {
            throw new NotImplementedException();
        }
    }

Can generics like this be represented in OpenAPI schema so that generated DTOs for languages with generic support (i.e. Java, TypeScript) would also have generic types?

1

1 Answers

0
votes

Yes, but you will lose information about generic origin of those classes.

Using swashbuckle this is excerpt from generated swagger.json:

 "DateTimeMessage": {
    "type": "object",
    "properties": {
      "id": {
        "type": "string",
        "nullable": true
      },
      "payload": {
        "type": "string",
        "format": "date-time"
      }
    },
    "additionalProperties": false
  },
  "BasicClientState": {
    "type": "object",
    "properties": {
      "count": {
        "type": "integer",
        "format": "int32"
      },
      "faultPercentage": {
        "type": "number",
        "format": "double"
      }
    },
    "additionalProperties": false
  },
  "BasicClientStateMessage": {
    "type": "object",
    "properties": {
      "id": {
        "type": "string",
        "nullable": true
      },
      "payload": {
        "$ref": "#/components/schemas/BasicClientState"
      }
    },
    "additionalProperties": false
  }