Skip to content

Add support for generic structs/enums/etc. #10

@LazyMechanic

Description

@LazyMechanic

opg does not support generic structs.

#[derive(Debug, serde::Serialize, opg::OpgModel)]
struct TestStruct<T: std::fmt::Debug> {
    inner: T,
}

#[derive(Debug, serde::Serialize, opg::OpgModel)]
struct TestSubStruct1 {
    a: i32,
    b: i64,
}

#[derive(Debug, serde::Serialize, opg::OpgModel)]
struct TestSubStruct2<'a> {
    c: &'a i32,
    d: String,
}

describe_api!(
    info: {
        title: "title",
        description: "description",
        version: "0.1.0",
    },
    paths: {
        ("some" / "path" / "one"): {
            GET: {
                description: "Get smth 1",
                200: TestStruct<TestSubStruct1>,
            },
        },
        ("some" / "path" / "two"): {
            GET: {
                description: "Get smth 1",
                200: TestStruct<TestSubStruct2<'static>>,
            },
        },
    }
)

Converts to this:

---
openapi: 3.0.3
info:
  title: title
  description: description
  version: 0.1.0
paths:
  /some/path/one:
    get:
      description: Get smth 1
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/TestStruct"
  /some/path/two:
    get:
      description: Get smth 1
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/TestStruct"
components:
  schemas:
    TestStruct:
      type: object
      properties:
        inner:
          $ref: "#/components/schemas/TestSubStruct1"
      required:
        - inner
    TestSubStruct1:
      type: object
      properties:
        a:
          type: integer
          format: int32
        b:
          type: integer
          format: int64
      required:
        - a
        - b

There is the only first TestStruct<TestSubStruct1> type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions