openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] Erlang-server schema validation failure for json string body

Open riccardomanfrin opened this issue 7 years ago • 5 comments

Description

Not sure if it's because of my spec, but I'm opening it to get some help. Starting from the swagger 2 API spec in the below yaml, I run the generator for erlang-server. THis produces the openapi.json priv file and .erl stuff. Among my APIs I have a post with a json string body. This API fails with the following info log:

Unable to process request for 'RootPost': {wrong_param,binary,schema,#{error => {schema_not_found,"#/components/schemas/binary"},type => schema_invalid}}

The failure occurs in *_api.erl file around here:

validate_with_schema(Body, Definition, ValidatorState) ->
    jesse_schema_validator:validate_with_state(
        [{<<"$ref">>, Definition}],
        Body,
        ValidatorState
    ).

I took deps from the README.md

    {jesse,  {git, "https://github.com/for-GET/jesse.git", {tag, "1.5.2"}}},
    {jsx,               {git, "https://github.com/talentdeficit/jsx.git", {tag, "2.9.0"}}},
    {cowboy,            {git, "https://github.com/ninenines/cowboy.git", {tag, "2.5.0"}}}
openapi-generator version

4.0.1-SNAPSHOT not a regration afaik

OpenAPI declaration file content or url

This is the swagger 2 input yaml:

paths:
  /:
    post:
      tags:
        - index
      description: "Create a new Table Index"
      parameters:
      - name: "index"
        in: "body"
        description: "Table Index to create up"
        schema:
          type: "string"
      responses:
        201:
          description: "New Index Created"

This is the openapi3 output json from the cli

"paths" : {
    "/" : {
      "post" : {
        "tags" : [ "index" ],
        "description" : "Create a new Table Index",
        "requestBody" : {
          "description" : "Table Index to create up",
          "content" : {
            "*/*" : {
              "schema" : {
                "type" : "string"
              }
            }
          },
          "required" : false
        },
        "responses" : {
          "201" : {
            "description" : "New Index Created",
            "content" : { }
          }
        },
        "x-codegen-request-body-name" : "index"
      }
    },
Command line used for generation
docker run --rm \
	-v $(pwd):/local \
	openapitools/openapi-generator-cli \
	generate \
	-i /local/priv/swagger.yaml \
	-g erlang-server \
	-o /local/out/
Steps to reproduce

Start application and curl -X POST "http://localhost:5678/" -H "Content-Type: application/json" -d '"hello"'

Suggest a fix

Possibly there should be no schema (no /components/schemas) to validate at all for a json string (?)

riccardomanfrin avatar May 17 '19 14:05 riccardomanfrin

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

auto-labeler avatar May 17 '19 14:05 auto-labeler

After some further investigation, hacking the output openapi.json file from this

"components" : {
    "schemas" : { }
  }

to this

 "components" : {
    "schemas" : {
      "binary": {
        "type": "string"
      }
    }
  }

appears to make the validator happy.. Not sure if I totally misunderstood everything with this..

riccardomanfrin avatar May 17 '19 14:05 riccardomanfrin

At the current state of the generator it seems like the requestBody can only have an object type as root, probably because it is encoded as a json and a string alone is not enough for a json object.

DonDi94 avatar May 22 '19 07:05 DonDi94

Thanks for clarifying about this, is this limitation going to be amended or is it by will?

riccardomanfrin avatar May 22 '19 07:05 riccardomanfrin

According to the specifications' example it should be possible to send just a string as a request body (https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#media-type-object), so I don't know ¯\_(ツ)_/¯

The server seem to accept only jsons anyway in the prepare_body function in the server's package_api.erl, so it could be a mix of templates' and generator's shortcomings.

DonDi94 avatar May 22 '19 11:05 DonDi94