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

Validate (and reject) recursive types #13

Open
stano45 opened this issue May 20, 2023 · 0 comments
Open

Validate (and reject) recursive types #13

stano45 opened this issue May 20, 2023 · 0 comments
Labels
Difficulty: medium 2 - 8 hours Type: bug Something isn't working

Comments

@stano45
Copy link
Collaborator

stano45 commented May 20, 2023

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
@stano45 stano45 added Type: bug Something isn't working Difficulty: easy 1 - 2 hours Difficulty: medium 2 - 8 hours and removed Type: bug Something isn't working Difficulty: easy 1 - 2 hours labels May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: medium 2 - 8 hours Type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant