18

Designing an API using editor.swagger.io I find myself unable to add a requestBody attribute, getting an error I cannot address:

Schema error at paths['/projects/{projectId}/user'].post
should NOT have additional properties
additionalProperty: requestBody
Jump to line 91

I don't understand what I'm doing wrong, especially after looking at the requestBody documentation. Research has brought me nothing other than the tendency for errors to be misleading.

EDIT: From what the answers here have shown, it looks like the editor is supposed to use OpenAPI 2.0, but actually expects 3.0 while returning errors for both. I'd use some help on what to use, given that I've included a

swagger: "2.0"

line at the beginning of the document. While testing with openapi: 3.0.0 as shown by @Mike in his answer, I just get more errors about allowed additional properties.

Here's what's generating the error, line 91 being post: .

/projects/{projectId}/user:
      post:
        tags:
        - projects
        summary: Modify project user.
        operationId: modifyProjectUser
        parameters:
        - name: projectId
          in: path
          description: ID of the project
          required: true
          type: integer
          format: int32
        requestBody:
          content: 
            application/json:
              schema:
              $ref: '#/definitions/User'
        responses:
          "200":
            description: Successful operation
            schema:
              type: array
              items:
                $ref: "#/definitions/User"
        security:
        - api_key: []
ilomax
  • 568
  • 1
  • 4
  • 24
  • 1
    What version of OpenAPI is it based on? The documentation link you provided states that for OpenAPI v2, requestBody isn't supported and that it uses body and formData parameters. (see here for example: https://swagger.io/docs/specification/2-0/describing-request-body/ ). Can you check what version you're using / that editor.swagger.io uses? – refreshfr Dec 04 '17 at 13:02
  • You are mixing 2.0 and 3.0 syntax. 2.0 specs use `swagger: '2.0'`, 3.0 specs use `openapi: 3.0.0`. – Helen Dec 04 '17 at 13:07
  • @refreshfr As indicated in https://swagger.io/specification/, Swagger 2.0 uses OAS 3.0, therefore making `requestBody` usable. In addition, when trying to use `in: body`, I get the following error: **allowedValues: header, formData, query, path**. – ilomax Dec 04 '17 at 13:09
  • @Helen, so what should I use in this case ? The Swagger documentation still gives me requestBody, and `in: body` doesn't work. I'm indeed confused. – ilomax Dec 04 '17 at 13:10
  • @ilomax https://swagger.io/specification is the 3.0 spec. The 2.0 spec is https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md. – Helen Dec 04 '17 at 13:11
  • Check out the examples at the link that @refreshfr posted - https://swagger.io/docs/specification/2-0/describing-request-body/ – Helen Dec 04 '17 at 13:13
  • Possible duplicate of [swagger: requestBody not allowed](https://stackoverflow.com/questions/46610891/swagger-requestbody-not-allowed) – Helen Dec 04 '17 at 13:15
  • @Helen So the line `swagger: "2.0"` I included means that I should use the example given for OAS 3.0, right ? The examples in the link posted by @refreshfr are for OAS 2.0, and when trying to use the `parameter in: body`, I get the error I reported above. Do you need more elements ? How can I be sure that I'm using the right things ? – ilomax Dec 04 '17 at 13:39

3 Answers3

18

I got clarifications from an external source, so here's what I've learned:

Specifying swagger: 2.0 also means that the OpenAPI Specification 2.0.0 is expected by the editor, whereas I thought it used OAS 3. I'm still unsure about why in: body did not work in the first place but I've added quotes around "body", which made the error disappear. Then I tried removing the quotes and it worked fine.

The editor doesn't seem very reliable when it comes to error reporting.

ilomax
  • 568
  • 1
  • 4
  • 24
7

This error message looked familiar. Try inserting a schema: underneath your parameter's required: line, then indent the type: and format: lines.

Since I have not yet set up my own SwaggerUI server. I took your code snippet and pasted it into SwaggerHub. Then I removed the $ref: lines just to further simplify the codebase. Here's a screenshot of the error-free result. enter image description here

Mike
  • 676
  • 1
  • 8
  • 17
  • Does the above fix your problem? – Mike Dec 04 '17 at 23:41
  • 1
    This did not really help. It actually show more errors. If you don't mind taking a look at [this screenshot](https://i.gyazo.com/fdc8646e0705ad535c899bbcf249e92d.png), you'll see the entire block as well as the exact errors. – ilomax Dec 05 '17 at 08:27
  • It should indeed. Thanks for noticing ! – ilomax Dec 07 '17 at 15:52
  • Thank you. It works, including defining the `parameters` on `path` level. See https://swagger.io/docs/specification/describing-parameters/#path-parameters — here the example contains `schema → type`. – Dmitriy Popov May 18 '20 at 13:37
  • This did not help but the image showed me the general layout and found a fix for my problem. I had `requestBody` under `response`. Sorry long day. – heretoinfinity Mar 25 '21 at 00:24
1

In my case since I was using openapi: 3.0.0. The schema $ref should be '#/components/schemas/{schemaname}' instead of '#/definitions/schemas/{schemaname}'

Skillz
  • 308
  • 4
  • 10