OpenAPITools / OpenAPITools/openapi-generator

[BUG] YAML fails validation; same JSON passes

Open
#7,218 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?

  • Have you validated the input using an OpenAPI validator (example)?

  • What's the version of OpenAPI Generator used?

  • Have you search for related issues/PRs?

  • What's the actual output vs expected output?

  • [Optional] Bounty to sponsor the fix (example)

Description

YAML fails to generated python code; JSON for the same spec works.

I think this has to do with the *id/&id references not being valid, but the YAML passes the validator. The same JSON works fine (see both below).

Errors:

openapi-generator generate -v -i test-schema.yaml -g python --package-name test -o test
[main] INFO  o.o.c.config.CodegenConfigurator -
VERBOSE MODE: ON. Additional debug options are injected
 - [debugOpenAPI] prints the OpenAPI specification as interpreted by the codegen
 - [debugModels] prints models passed to the template engine
 - [debugOperations] prints operations passed to the template engine
 - [debugSupportingFiles] prints additional data passed to the template engine
Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
 | Error count: 2, Warning count: 0
Errors:
	-attribute paths.'/_/api/users/{username}/'(put).requestBody.content.'application/x-www-form-urlencoded'.schema is not of type `object`
	-attribute paths.'/_/api/users/{username}/'(put).requestBody.content.'multipart/form-data'.schema is not of type `object`

	at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:480)
	at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:507)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:423)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:61)
openapi-generator version
openapi-generator-cli 4.3.1
  commit : 003165c
  built  : 2020-05-06T09:38:39Z
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url

Given this YAML:

openapi: 3.0.2
info:
  title: REST API
  version: 1.0.0a
  description: API
paths:
  /_/api/users/{username}/:
    put:
      operationId: updateQUser
      description: API endpoint that allows users to be viewed or edited.
      parameters:
      - name: username
        in: path
        required: true
        description: Required. 150 characters or fewer. Letters, digits and @/./+/-/_
          only.
        schema:
          type: string
      requestBody:
        content:
          application/json:
            schema: &id002
              properties:
                username:
                  type: string
                  description: Required. 150 characters or fewer. Letters, digits
                    and @/./+/-/_ only.
                  pattern: ^[\w.@+-]+\Z
                  maxLength: 150
              required:
              - username
          application/x-www-form-urlencoded:
            schema: *id002
          multipart/form-data:
            schema: *id002
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  url:
                    type: string
                    readOnly: true
                  username:
                    type: string
                    description: Required. 150 characters or fewer. Letters, digits
                      and @/./+/-/_ only.
                    pattern: ^[\w.@+-]+\Z
                    maxLength: 150
                  repo_owner:
                    type: array
                    items:
                      type: string
                    readOnly: true
                required:
                - username
          description: ''

And the JSON:


        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "properties": {
                  "username": {
                    "type": "string",
                    "description": "Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
                    "pattern": "^[\\w.@+-]+\\Z",
                    "maxLength": 150
                  }
                },
                "required": [
                  "username"
                ]
              }
            },
            "application/x-www-form-urlencoded": {
              "schema": {
                "properties": {
                  "username": {
                    "type": "string",
                    "description": "Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
                    "pattern": "^[\\w.@+-]+\\Z",
                    "maxLength": 150
                  }
                },
                "required": [
                  "username"
                ]
              }
            },
            "multipart/form-data": {
              "schema": {
                "properties": {
                  "username": {
                    "type": "string",
                    "description": "Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
                    "pattern": "^[\\w.@+-]+\\Z",
                    "maxLength": 150
                  }
                },
                "required": [
                  "username"
                ]
              }
            }
          }
        },

Generation Details

openapi-generator generate -v -i schema.yaml -g python --package-name test_package -o api

Steps to reproduce

Run the above YAML through openapi-generator.

Related issues/PRs

https://github.com/encode/django-rest-framework/issues/7479

Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the provided openapi-generator command and YAML spec, then compare validation of the YAML aliases with the equivalent JSON schemas. Trace the requestBody schema validation path for form-encoded and multipart content. Done means the YAML and equivalent JSON produce consistent validation results, with a regression test for the reported spec.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, python, yaml
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.