opg icon indicating copy to clipboard operation
opg copied to clipboard

Add support for generic structs/enums/etc.

Open LazyMechanic opened this issue 3 years ago • 1 comments

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.

LazyMechanic avatar Aug 20 '21 13:08 LazyMechanic

Is there any news about it? It looks pretty useful

AndreyErmilov avatar Jul 15 '22 10:07 AndreyErmilov