diff --git a/README.md b/README.md index ff47c88ff23..f02a8ada352 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ OpenAPI Generator allows generation of API client libraries (SDK generation), se | | Languages/Frameworks | | -------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | **API clients** | **ActionScript**, **Ada**, **Apex**, **Bash**, **C**, **C#** (.net 2.0, 3.5 or later, .NET Standard 1.3 - 2.0, .NET Core 2.0, .NET 5.0. Libraries: RestSharp, HttpClient), **C++** (Arduino, cpp-restsdk, Qt5, Tizen, Unreal Engine 4), **Clojure**, **Crystal**, **Dart**, **Elixir**, **Elm**, **Eiffel**, **Erlang**, **Go**, **Groovy**, **Haskell** (http-client, Servant), **Java** (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured, Spring 5 Web Client, MicroProfile Rest Client), **k6**, **Kotlin**, **Lua**, **Nim**, **Node.js/JavaScript** (ES5, ES6, AngularJS with Google Closure Compiler annotations, Flow types, Apollo GraphQL DataStore), **Objective-C**, **OCaml**, **Perl**, **PHP**, **PowerShell**, **Python**, **R**, **Ruby**, **Rust** (hyper, reqwest, rust-server), **Scala** (akka, http4s, scalaz, sttp, swagger-async-httpclient), **Swift** (2.x, 3.x, 4.x, 5.x), **Typescript** (AngularJS, Angular (2.x - 11.x), Aurelia, Axios, Fetch, Inversify, jQuery, Nestjs, Node, redux-query, Rxjs) | -| **Server stubs** | **Ada**, **C#** (ASP.NET Core, NancyFx), **C++** (Pistache, Restbed, Qt5 QHTTPEngine), **Erlang**, **F#** (Giraffe), **Go** (net/http, Gin, Echo), **Haskell** (Servant), **Java** (MSF4J, Spring, Undertow, JAX-RS: CDI, CXF, Inflector, Jersey, RestEasy, Play Framework, [PKMST](https://github.com/ProKarma-Inc/pkmst-getting-started-examples), [Vert.x](https://vertx.io/)), **Kotlin** (Spring Boot, Ktor, Vertx), **PHP** (Laravel, Lumen, Slim, Silex, [Symfony](https://symfony.com/), [Zend Expressive](https://github.com/zendframework/zend-expressive)), **Python** (Flask), **NodeJS**, **Ruby** (Sinatra, Rails5), **Rust** (rust-server), **Scala** (Akka, [Finch](https://github.com/finagle/finch), [Lagom](https://github.com/lagom/lagom), [Play](https://www.playframework.com/), Scalatra) | +| **Server stubs** | **Ada**, **C#** (ASP.NET Core, NancyFx), **C++** (Pistache, Restbed, Qt5 QHTTPEngine), **Erlang**, **F#** (Giraffe), **Go** (net/http, Gin, Echo), **Haskell** (Servant), **Java** (MSF4J, Spring, Undertow, JAX-RS: CDI, CXF, Inflector, Jersey, RestEasy, Play Framework, [PKMST](https://github.com/ProKarma-Inc/pkmst-getting-started-examples), [Vert.x](https://vertx.io/)), **Kotlin** (Spring Boot, Ktor, Vertx), **PHP** (Laravel, Lumen, Slim, Silex, [Symfony](https://symfony.com/), [Zend Expressive](https://github.com/zendframework/zend-expressive)), **Python** (FastAPI, Flask), **NodeJS**, **Ruby** (Sinatra, Rails5), **Rust** (rust-server), **Scala** (Akka, [Finch](https://github.com/finagle/finch), [Lagom](https://github.com/lagom/lagom), [Play](https://www.playframework.com/), Scalatra) | | **API documentation generators** | **HTML**, **Confluence Wiki**, **Asciidoc**, **Markdown**, **PlantUML** | | **Configuration files** | [**Apache2**](https://httpd.apache.org/) | | **Others** | **GraphQL**, **JMeter**, **Ktorm**, **MySQL Schema**, **Protocol Buffer**, **WSDL** | @@ -951,6 +951,7 @@ Here is a list of template creators: * PHP Slim: @jfastnacht * PHP Symfony: @ksm2 * PHP Zend Expressive (with Path Handler): @Articus + * Python FastAPI: @krjakbrjak * Python AIOHTTP: @Jyhess * Ruby on Rails 5: @zlx * Rust (rust-server): @metaswitch diff --git a/docs/generators.md b/docs/generators.md index f1cec8c6687..2531acc9e67 100644 --- a/docs/generators.md +++ b/docs/generators.md @@ -120,6 +120,7 @@ The following generators are available: * [php-symfony](generators/php-symfony.md) * [python-aiohttp](generators/python-aiohttp.md) * [python-blueplanet](generators/python-blueplanet.md) +* [python-fastapi (beta)](generators/python-fastapi.md) * [python-flask](generators/python-flask.md) * [ruby-on-rails](generators/ruby-on-rails.md) * [ruby-sinatra](generators/ruby-sinatra.md) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFastAPIServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFastAPIServerCodegen.java index f4e69664724..f973a6f2609 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFastAPIServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFastAPIServerCodegen.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.openapitools.codegen.languages; import com.fasterxml.jackson.core.JsonGenerator; @@ -10,6 +26,9 @@ import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.Schema; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.*; +import org.openapitools.codegen.meta.GeneratorMetadata; +import org.openapitools.codegen.meta.Stability; +import org.openapitools.codegen.meta.features.*; import org.openapitools.codegen.utils.ModelUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,12 +72,16 @@ public class PythonFastAPIServerCodegen extends AbstractPythonCodegen { @Override public String getHelp() { - return "Generates a python server (FastAPI)."; + return "Generates a Python FastAPI server (beta)."; } public PythonFastAPIServerCodegen() { super(); + generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata) + .stability(Stability.BETA) + .build(); + SimpleModule simpleModule = new SimpleModule(); simpleModule.addKeySerializer(String.class, new SnakeCaseKeySerializer()); simpleModule.addSerializer(Boolean.class, new PythonBooleanSerializer()); @@ -115,7 +138,7 @@ public class PythonFastAPIServerCodegen extends AbstractPythonCodegen { // Add __init__.py to all sub-folders under namespace pkg StringBuilder namespacePackagePath = new StringBuilder(SRC_DIR + File.separator + StringUtils.substringBefore(packageName, ".")); - for (String tmp: StringUtils.split(StringUtils.substringAfter(packageName, "."), '.')) { + for (String tmp : StringUtils.split(StringUtils.substringAfter(packageName, "."), '.')) { namespacePackagePath.append(File.separator).append(tmp); supportingFiles.add(new SupportingFile("__init__.mustache", namespacePackagePath.toString(), "__init__.py")); } @@ -181,7 +204,7 @@ public class PythonFastAPIServerCodegen extends AbstractPythonCodegen { } List securityMethods = operation.authMethods; if (securityMethods != null) { - for (final CodegenSecurity securityMethod: securityMethods) { + for (final CodegenSecurity securityMethod : securityMethods) { securityImports.add(securityMethod.name); } } @@ -193,7 +216,8 @@ public class PythonFastAPIServerCodegen extends AbstractPythonCodegen { // If fails, use the original string. try { Map result = MAPPER.readValue(example.get("example"), - new TypeReference>(){}); + new TypeReference>() { + }); operation.bodyParam.example = MAPPER.writeValueAsString(result); } catch (IOException e) { operation.bodyParam.example = example.get("example"); diff --git a/modules/openapi-generator/src/main/resources/python-fastapi/security_api.mustache b/modules/openapi-generator/src/main/resources/python-fastapi/security_api.mustache index f925edad438..77bceb11cfa 100644 --- a/modules/openapi-generator/src/main/resources/python-fastapi/security_api.mustache +++ b/modules/openapi-generator/src/main/resources/python-fastapi/security_api.mustache @@ -34,7 +34,7 @@ oauth2_password = OAuth2PasswordBearer( oauth2_code = OAuth2AuthorizationCodeBearer( authorizationUrl="{{authorizationUrl}}", tokenUrl="{{tokenUrl}}", - refreshUrlUrl="{{refreshUrl}}", + refreshUrl="{{refreshUrl}}", scopes={ {{#scopes}} "{{scope}}": "{{description}}", diff --git a/samples/server/petstore/python-fastapi/openapi.yaml b/samples/server/petstore/python-fastapi/openapi.yaml index e8e287f9991..477694ade00 100644 --- a/samples/server/petstore/python-fastapi/openapi.yaml +++ b/samples/server/petstore/python-fastapi/openapi.yaml @@ -834,10 +834,7 @@ components: write:pets: modify pets in your account read:pets: read your pets type: oauth2 - x-tokenInfoFunc: .security_controller_.info_from_petstore_auth - x-scopeValidateFunc: .security_controller_.validate_scope_petstore_auth api_key: in: header name: api_key type: apiKey - x-apikeyInfoFunc: .security_controller_.info_from_api_key