Skip to content

Commit

Permalink
[ python-fastapi] minor improvements (#9622)
Browse files Browse the repository at this point in the history
* minor python-fastapi improvements

* update doc

* fix refreshUrl
  • Loading branch information
wing328 committed May 31, 2021
1 parent c4df343 commit 32a0450
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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** |
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,6 +26,9 @@
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;
Expand Down Expand Up @@ -53,12 +72,16 @@ public CodegenType getTag() {

@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());
Expand Down Expand Up @@ -115,7 +138,7 @@ public void processOpts() {

// 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"));
}
Expand Down Expand Up @@ -181,7 +204,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}
List<CodegenSecurity> securityMethods = operation.authMethods;
if (securityMethods != null) {
for (final CodegenSecurity securityMethod: securityMethods) {
for (final CodegenSecurity securityMethod : securityMethods) {
securityImports.add(securityMethod.name);
}
}
Expand All @@ -193,7 +216,8 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
// If fails, use the original string.
try {
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);
} catch (IOException e) {
operation.bodyParam.example = example.get("example");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ oauth2_password = OAuth2PasswordBearer(
oauth2_code = OAuth2AuthorizationCodeBearer(
authorizationUrl="{{authorizationUrl}}",
tokenUrl="{{tokenUrl}}",
refreshUrlUrl="{{refreshUrl}}",
refreshUrl="{{refreshUrl}}",
scopes={
{{#scopes}}
"{{scope}}": "{{description}}",
Expand Down
3 changes: 0 additions & 3 deletions samples/server/petstore/python-fastapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 32a0450

Please sign in to comment.