Skip to content

Validate (and reject) recursive types #13

Open
@stano45

Description

@stano45
Collaborator

Example spec:

      message:
        name: userSignedUp
        title: User signed up event
        summary: Inform about a new user registration in the system
        contentType: application/json
        payload:
          type: object
          properties:
            nested:
              type: object
              properties:
                firstName:
                  type: number
                  description: "foo"
                lastName:
                  type: boolean
                  description: "bar"
                email:
                  type: string
                  format: email
                  description: "baz"
                createdAt:
                  type: string
                  format: date-time
                  description: "foo"
                evenMoreNested:
                  type: object
                  properties:
                    nested:
                      type: object
                      properties:
                        firstName:
                          type: number
                          description: "foo"
                        lastName:
                          type: boolean
                          description: "bar"
                        email:
                          type: string
                          format: email
                          description: "baz"
                        createdAt:
                          type: string
                          format: date-time
                          description: "foo"
            firstName:
              type: string
              description: "foo"
            lastName:
              type: string
              description: "bar"
            email:
              type: string
              format: email
              description: "baz"
            createdAt:
              type: string
              format: date-time
              description: "foo"

Generates:

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EvenMoreNested {
    pub nested: Nested,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct UserSignedup {
    pub nested: Nested,
    pub firstName: String,
    pub lastName: String,
    pub email: String,
    pub createdAt: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Nested {
    pub firstName: f64,
    pub lastName: bool,
    pub email: String,
    pub createdAt: String,
    pub evenMoreNested: EvenMoreNested,
}

Which results in:

error[E0072]: recursive types `EvenMoreNested` and `Nested` have infinite size
  --> src/main.rs:5:1
   |
5  | pub struct EvenMoreNested {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
6  |     pub nested: Nested,
   |                 ------ recursive without indirection
...
19 | pub struct Nested {
   | ^^^^^^^^^^^^^^^^^
...
24 |     pub evenMoreNested: EvenMoreNested,
   |                         -------------- recursive without indirection
   |
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
   |
6  ~     pub nested: Box<Nested>,
7  | }
 ...
23 |     pub createdAt: String,
24 ~     pub evenMoreNested: Box<EvenMoreNested>,
   |

For more information about this error, try `rustc --explain E0072`.
error: could not compile `email_server` due to previous error

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @stano45

        Issue actions

          Validate (and reject) recursive types · Issue #13 · Programmierpraktikum-MVA/AsyncAPI