question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValidationError fail OpenAPI Schema validation

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

Description

  • Running openapi-generator against the schema generated by FastAPI fails.
  • ValidationError schema cannot be processed.
  • Generation fails with FastAPI v0.75.0 or higher
  • Generated successfully with FastAPI v0.74.1
$ openapi-generator-cli generate -g typescript-axios -i http://localhost:8000/openapi.json

[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: typescript-axios (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'typescript-axios' is considered stable.
[main] INFO  o.o.c.l.AbstractTypeScriptClientCodegen - Hint: Environment variable 'TS_POST_PROCESS_FILE' (optional) not defined. E.g. to format the source code, please try 'export TS_POST_PROCESS_FILE="/usr/local/bin/prettier --write"' (Linux/Mac)
[main] INFO  o.o.c.l.AbstractTypeScriptClientCodegen - Note: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] WARN  o.o.c.l.AbstractTypeScriptClientCodegen - The import is a union type. Consider using the toModelImportMap method.
Exception in thread "main" java.lang.RuntimeException: Could not process model 'ValidationError'.Please make sure that your schema is correct!
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:492)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:888)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.StringIndexOutOfBoundsException: begin 1, end 0, length 0
        at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3751)
        at java.base/java.lang.String.substring(String.java:1907)
        at java.base/java.lang.String.substring(String.java:1880)
        at org.openapitools.codegen.languages.TypeScriptAxiosClientCodegen.postProcessModels(TypeScriptAxiosClientCodegen.java:233)
        at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1302)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:487)
        ... 4 more

Process finished with exit code 1

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.75.2

Python Version

3.10.4

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
flxdotcommented, May 16, 2022

Well, it is due to this change: https://github.com/tiangolo/fastapi/pull/4806

But I would rather argue this is an issue of openapi-generator, as the schema is valid according to the specification of OpenAPI. There is even an open issue for that: https://github.com/OpenAPITools/openapi-generator/issues/4576

0reactions
gaganpreetcommented, May 23, 2022

This issue with typescript-axios generator is fixed in the main branch of openapi generator, but it’s not packaged yet. Using git bisect I pinned the fix down to this commit

Edit: Downgrading to 5.3.x also works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swagger schema validation failed
trying to import a URL and got this error: ``` Swagger schema validation failed. Data does not match any schemas from 'anyOf' at ......
Read more >
OpenAPI Errors - rororo 3.0.1 documentation
Response data validation error. Document below describes how exactly those errors handled, and what changes to OpenAPI Schema you might add to support...
Read more >
express-openapi-validator - npm
Automatically validate API requests and responses with OpenAPI 3 and Express.. Latest version: 5.0.0, last published: a month ago.
Read more >
OpenAPI Validation error - ServiceStack Customer Forums
Error Details: The request failed with error: 'Parsing error(s): JSON is valid against more than one schema from 'oneOf'. No valid schemas. Path ......
Read more >
JSON validation - IBM
Interpreting validation errors ... A validation error occurs when the JSON document does not conform to the rules that are defined in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found