[ python-fastapi] minor improvements (#9622)

* minor python-fastapi improvements

* update doc

* fix refreshUrl
This commit is contained in:
William Cheng 2021-05-31 17:00:23 +08:00 committed by GitHub
parent c4df343052
commit 32a045060e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 9 deletions

View File

@ -68,7 +68,7 @@ OpenAPI Generator allows generation of API client libraries (SDK generation), se
| | Languages/Frameworks | | | 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) | | **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** | | **API documentation generators** | **HTML**, **Confluence Wiki**, **Asciidoc**, **Markdown**, **PlantUML** |
| **Configuration files** | [**Apache2**](https://httpd.apache.org/) | | **Configuration files** | [**Apache2**](https://httpd.apache.org/) |
| **Others** | **GraphQL**, **JMeter**, **Ktorm**, **MySQL Schema**, **Protocol Buffer**, **WSDL** | | **Others** | **GraphQL**, **JMeter**, **Ktorm**, **MySQL Schema**, **Protocol Buffer**, **WSDL** |
@ -951,6 +951,7 @@ Here is a list of template creators:
* PHP Slim: @jfastnacht * PHP Slim: @jfastnacht
* PHP Symfony: @ksm2 * PHP Symfony: @ksm2
* PHP Zend Expressive (with Path Handler): @Articus * PHP Zend Expressive (with Path Handler): @Articus
* Python FastAPI: @krjakbrjak
* Python AIOHTTP: @Jyhess * Python AIOHTTP: @Jyhess
* Ruby on Rails 5: @zlx * Ruby on Rails 5: @zlx
* Rust (rust-server): @metaswitch * Rust (rust-server): @metaswitch

View File

@ -120,6 +120,7 @@ The following generators are available:
* [php-symfony](generators/php-symfony.md) * [php-symfony](generators/php-symfony.md)
* [python-aiohttp](generators/python-aiohttp.md) * [python-aiohttp](generators/python-aiohttp.md)
* [python-blueplanet](generators/python-blueplanet.md) * [python-blueplanet](generators/python-blueplanet.md)
* [python-fastapi (beta)](generators/python-fastapi.md)
* [python-flask](generators/python-flask.md) * [python-flask](generators/python-flask.md)
* [ruby-on-rails](generators/ruby-on-rails.md) * [ruby-on-rails](generators/ruby-on-rails.md)
* [ruby-sinatra](generators/ruby-sinatra.md) * [ruby-sinatra](generators/ruby-sinatra.md)

View File

@ -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; package org.openapitools.codegen.languages;
import com.fasterxml.jackson.core.JsonGenerator; 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 io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*; 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.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -53,12 +72,16 @@ public class PythonFastAPIServerCodegen extends AbstractPythonCodegen {
@Override @Override
public String getHelp() { public String getHelp() {
return "Generates a python server (FastAPI)."; return "Generates a Python FastAPI server (beta).";
} }
public PythonFastAPIServerCodegen() { public PythonFastAPIServerCodegen() {
super(); super();
generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
.stability(Stability.BETA)
.build();
SimpleModule simpleModule = new SimpleModule(); SimpleModule simpleModule = new SimpleModule();
simpleModule.addKeySerializer(String.class, new SnakeCaseKeySerializer()); simpleModule.addKeySerializer(String.class, new SnakeCaseKeySerializer());
simpleModule.addSerializer(Boolean.class, new PythonBooleanSerializer()); 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 // Add __init__.py to all sub-folders under namespace pkg
StringBuilder namespacePackagePath = new StringBuilder(SRC_DIR + File.separator + StringUtils.substringBefore(packageName, ".")); 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); namespacePackagePath.append(File.separator).append(tmp);
supportingFiles.add(new SupportingFile("__init__.mustache", namespacePackagePath.toString(), "__init__.py")); supportingFiles.add(new SupportingFile("__init__.mustache", namespacePackagePath.toString(), "__init__.py"));
} }
@ -181,7 +204,7 @@ public class PythonFastAPIServerCodegen extends AbstractPythonCodegen {
} }
List<CodegenSecurity> securityMethods = operation.authMethods; List<CodegenSecurity> securityMethods = operation.authMethods;
if (securityMethods != null) { if (securityMethods != null) {
for (final CodegenSecurity securityMethod: securityMethods) { for (final CodegenSecurity securityMethod : securityMethods) {
securityImports.add(securityMethod.name); securityImports.add(securityMethod.name);
} }
} }
@ -193,7 +216,8 @@ public class PythonFastAPIServerCodegen extends AbstractPythonCodegen {
// If fails, use the original string. // If fails, use the original string.
try { try {
Map<String, Object> result = MAPPER.readValue(example.get("example"), Map<String, Object> result = MAPPER.readValue(example.get("example"),
new TypeReference<Map<String, Object>>(){}); new TypeReference<Map<String, Object>>() {
});
operation.bodyParam.example = MAPPER.writeValueAsString(result); operation.bodyParam.example = MAPPER.writeValueAsString(result);
} catch (IOException e) { } catch (IOException e) {
operation.bodyParam.example = example.get("example"); operation.bodyParam.example = example.get("example");

View File

@ -34,7 +34,7 @@ oauth2_password = OAuth2PasswordBearer(
oauth2_code = OAuth2AuthorizationCodeBearer( oauth2_code = OAuth2AuthorizationCodeBearer(
authorizationUrl="{{authorizationUrl}}", authorizationUrl="{{authorizationUrl}}",
tokenUrl="{{tokenUrl}}", tokenUrl="{{tokenUrl}}",
refreshUrlUrl="{{refreshUrl}}", refreshUrl="{{refreshUrl}}",
scopes={ scopes={
{{#scopes}} {{#scopes}}
"{{scope}}": "{{description}}", "{{scope}}": "{{description}}",

View File

@ -834,10 +834,7 @@ components:
write:pets: modify pets in your account write:pets: modify pets in your account
read:pets: read your pets read:pets: read your pets
type: oauth2 type: oauth2
x-tokenInfoFunc: .security_controller_.info_from_petstore_auth
x-scopeValidateFunc: .security_controller_.validate_scope_petstore_auth
api_key: api_key:
in: header in: header
name: api_key name: api_key
type: apiKey type: apiKey
x-apikeyInfoFunc: .security_controller_.info_from_api_key