Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LazyMechanic opened this issue Aug 20, 2021 · 1 comment
Open

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

LazyMechanic opened this issue Aug 20, 2021 · 1 comment

Comments

@LazyMechanic
Copy link

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.

@AndreyErmilov
Copy link

Is there any news about it? It looks pretty useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants