Repository: wafflespeanut/paperclip
Pull request #254 (2020-10-14 09:53:46)

Type "object" is missing in "definitions"
This PR, for the example [here](https://github.com/wafflespeanut/paperclip/blob/master/book/actix-plugin.md), instead of creating a yaml like:

```
swagger: '2.0'
definitions:
  Pet:
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
    required:
      - name
paths:
  /pets:
    post:
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/Pet'
      parameters:
        - in: body
          name: body
          required: true
          schema:
            $ref: '#/definitions/Pet'
info:
  version: ''
  title: ''
```

will create this, note the "type: object" after the "Pet" definition:

```
swagger: '2.0'
definitions:
  Pet:
    type: object
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
    required:
      - name
paths:
  /pets:
    post:
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/Pet'
      parameters:
        - in: body
          name: body
          required: true
          schema:
            $ref: '#/definitions/Pet'
info:
  version: ''
  title: ''
```


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 390a0161..7b7dc1ee 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,7 +18,7 @@ path = "src/bin/main.rs"
 required-features = ["cli"]
 
 [dependencies]
-env_logger = { version = "0.7", optional = true }
+env_logger = { version = "0.8", optional = true }
 git2 = { version = "0.13", optional = true }
 heck = { version = "0.3", optional = true }
 http = { version = "0.2", optional = true }
@@ -52,7 +52,7 @@ uuid_dev = { version = "0.8", features = ["serde"], package = "uuid" }
 reqwest = { version = "0.10", features = ["blocking", "json"] }
 log = { version = "0.4", features = ["kv_unstable"] }
 insta = "1.0"
-env_logger = "0.7"
+env_logger = "0.8"
 
 [features]
 # actix-web support
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 42638065..05228e88 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -21,7 +21,7 @@ log = { version = "0.4", optional = true }
 mime = "0.3"
 paperclip-macros = { path = "../macros", version = "0.4.0" }
 parking_lot = { version = ">=0.10,<0.12", features = ["serde"] }
-pin-project = { version = "0.4", optional = true }
+pin-project = { version = "1.0", optional = true }
 rust_decimal = { version = "1", optional = true }
 regex = "1.3"
 serde = { version = "1.0", features = ["derive", "rc"] }
diff --git a/macros/src/actix.rs b/macros/src/actix.rs
index 3783b884..1eb16fe2 100644
--- a/macros/src/actix.rs
+++ b/macros/src/actix.rs
@@ -481,27 +481,32 @@ pub fn emit_v2_definition(input: TokenStream) -> TokenStream {
     let mut props_gen = quote! {};
 
     match &item_ast.data {
-        Data::Struct(ref s) => match &s.fields {
-            Fields::Named(ref f) => handle_field_struct(f, &props, &mut props_gen),
-            Fields::Unnamed(ref f) => {
-                if f.unnamed.len() == 1 {
-                    handle_unnamed_field_struct(f, &mut props_gen)
-                } else {
+        Data::Struct(ref s) => {
+            props_gen.extend(quote!(
+                schema.data_type = Some(DataType::Object);
+            ));
+            match &s.fields {
+                Fields::Named(ref f) => handle_field_struct(f, &props, &mut props_gen),
+                Fields::Unnamed(ref f) => {
+                    if f.unnamed.len() == 1 {
+                        handle_unnamed_field_struct(f, &mut props_gen)
+                    } else {
+                        emit_warning!(
+                            f.span().unwrap(),
+                            "tuple structs do not have named fields and hence will have empty schema.";
+                            help = "{}", &*EMPTY_SCHEMA_HELP;
+                        );
+                    }
+                }
+                Fields::Unit => {
                     emit_warning!(
-                        f.span().unwrap(),
-                        "tuple structs do not have named fields and hence will have empty schema.";
+                        s.struct_token.span().unwrap(),
+                        "unit structs do not have any fields and hence will have empty schema.";
                         help = "{}", &*EMPTY_SCHEMA_HELP;
                     );
                 }
             }
-            Fields::Unit => {
-                emit_warning!(
-                    s.struct_token.span().unwrap(),
-                    "unit structs do not have any fields and hence will have empty schema.";
-                    help = "{}", &*EMPTY_SCHEMA_HELP;
-                );
-            }
-        },
+        }
         Data::Enum(ref e) => handle_enum(e, &props, &mut props_gen),
         Data::Union(ref u) => emit_error!(
             u.union_token.span().unwrap(),


Test patch:
diff --git a/tests/test_app.rs b/tests/test_app.rs
index 37f3b2c2..10edf836 100644
--- a/tests/test_app.rs
+++ b/tests/test_app.rs
@@ -168,7 +168,8 @@ fn test_simple_app() {
                           "type": "string"
                         }
                       },
-                      "required":["birthday", "class", "name"]
+                      "required":["birthday", "class", "name"],
+                      "type":"object"
                     }
                   },
                   "paths": {
@@ -488,14 +489,16 @@ fn test_params() {
                                 },
                                 "yaml": {
                                 }
-                            }
+                            },
+                            "type":"object"
                         },
                         "BadgeBodyPatch": {
                             "properties": {
                                 "json": {
                                     "description": "JSON value",
                                 }
-                            }
+                            },
+                            "type":"object"
                         }
                     },
                     "info": {
@@ -929,7 +932,8 @@ fn test_map_in_out() {
                                        "required":[
                                           "data",
                                           "id"
-                                       ]
+                                       ],
+                                       "type":"object"
                                     },
                                     "type":"array"
                                  },
@@ -938,7 +942,8 @@ fn test_map_in_out() {
                            },
                            "required":[
                               "folders"
-                           ]
+                           ],
+                           "type":"object"
                         },
                         "Filter":{
                            "properties":{
@@ -955,7 +960,8 @@ fn test_map_in_out() {
                            },
                            "required":[
                               "folders"
-                           ]
+                           ],
+                           "type":"object"
                         },
                         "Image":{
                            "properties":{
@@ -970,7 +976,8 @@ fn test_map_in_out() {
                            "required":[
                               "data",
                               "id"
-                           ]
+                           ],
+                           "type":"object"
                         }
                      },
                      "info":{
@@ -1114,7 +1121,8 @@ fn test_serde_flatten() {
                                   "data",
                                   "id",
                                   "time"
-                                ]
+                                ],
+                                 "type":"object"
                               },
                               "type": "array"
                             },
@@ -1137,7 +1145,8 @@ fn test_serde_flatten() {
                           "required": [
                             "data",
                             "paging"
-                          ]
+                          ],
+                          "type":"object"
                         }
                       },
                       "info": {
@@ -1253,7 +1262,8 @@ fn test_list_in_out() {
                           "type": "string"
                         }
                       },
-                      "required":["birthday", "class", "name"]
+                      "required":["birthday", "class", "name"],
+                      "type":"object"
                     }
                   },
                   "paths": {
@@ -1359,7 +1369,8 @@ fn test_tags() {
                             "required":[
                                 "data",
                                 "id"
-                            ]
+                            ],
+                            "type":"object"
                         }
                     },
                     "info":{
@@ -1491,7 +1502,8 @@ fn test_impl_traits() {
                           "type": "string"
                         }
                       },
-                      "required":["birthday", "class", "name"]
+                      "required":["birthday", "class", "name"],
+                      "type":"object"
                     }
                   },
                   "paths": {
@@ -1614,7 +1626,8 @@ fn test_operation_with_generics() {
                              "birthday",
                               "class",
                               "name"
-                           ]
+                           ],
+                           "type":"object"
                         }
                      },
                      "info":{
@@ -1772,7 +1785,8 @@ fn test_operations_documentation() {
                           "type": "string"
                         }
                       },
-                      "required":["birthday", "class", "name"]
+                      "required":["birthday", "class", "name"],
+                      "type":"object"
                     }
                   },
                   "paths": {
@@ -1915,7 +1929,8 @@ fn test_operations_macro_attributes() {
                                 "birthday",
                                 "class",
                                 "name"
-                            ]
+                            ],
+                            "type":"object"
                         }
                     },
                     "info": {
@@ -2222,7 +2237,8 @@ fn test_errors_app() {
                           "type": "string"
                         }
                       },
-                      "required":["birthday", "class", "name"]
+                      "required":["birthday", "class", "name"],
+                      "type":"object"
                     }
                   },
                   "paths": {
@@ -2383,7 +2399,8 @@ fn test_security_app() {
                           "type": "string"
                         }
                       },
-                      "required":["birthday", "class", "name"]
+                      "required":["birthday", "class", "name"],
+                      "type":"object"
                     }
                   },
                   "paths": {