Skip to content

Commit 2671b5b

Browse files
author
Julien Sagnard
committed
Add aiohttp server generator
1 parent a224c60 commit 2671b5b

File tree

83 files changed

+4494
-112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+4494
-112
lines changed

bin/python-aiohttp-petstore.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=`ls -ld "$SCRIPT"`
8+
link=`expr "$ls" : '.*-> \(.*\)$'`
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=`dirname "$SCRIPT"`/"$link"
13+
fi
14+
done
15+
16+
if [ ! -d "${APP_DIR}" ]; then
17+
APP_DIR=`dirname "$SCRIPT"`/..
18+
APP_DIR=`cd "${APP_DIR}"; pwd`
19+
fi
20+
21+
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
22+
23+
if [ ! -f "$executable" ]
24+
then
25+
mvn -B clean package
26+
fi
27+
28+
# if you've executed sbt assembly previously it will use that instead.
29+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
30+
ags="generate -t modules/openapi-generator/src/main/resources/python-aiohttp -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g python-aiohttp -o samples/server/petstore/python-aiohttp -Dservice $@"
31+
32+
java $JAVA_OPTS -jar $executable $ags

bin/python-flask-petstore-python2.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/
3030
#ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g python-flask -o samples/server/petstore/flaskConnexion-python2 -DsupportPython2=true $@"
3131
ags="generate -t modules/openapi-generator/src/main/resources/flaskConnexion -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g python-flask -o samples/server/petstore/flaskConnexion-python2 -c bin/supportPython2.json -D service $@"
3232

33-
rm -rf samples/server/petstore/flaskConnexion-python2/*
3433
java $JAVA_OPTS -jar $executable $ags

bin/python-flask-petstore.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,4 @@ fi
2929
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
3030
ags="generate -t modules/openapi-generator/src/main/resources/flaskConnexion -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g python-flask -o samples/server/petstore/flaskConnexion -Dservice $@"
3131

32-
rm -rf samples/server/petstore/flaskConnexion/*
3332
java $JAVA_OPTS -jar $executable $ags

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenSecurity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
public class CodegenSecurity {
2828
public String name;
2929
public String type;
30+
public String scheme;
3031
public Boolean hasMore, isBasic, isOAuth, isApiKey;
32+
// is Basic is true for all http authentication type. Those are to differentiate basic and bearer authentication
33+
public Boolean isBasicBasic, isBasicBearer;
3134
public Map<String, Object> vendorExtensions = new HashMap<String, Object>();
3235
// ApiKey specific
3336
public String keyParamName;

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3228,6 +3228,8 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> securitySc
32283228
cs.name = key;
32293229
cs.type = securityScheme.getType().toString();
32303230
cs.isCode = cs.isPassword = cs.isApplication = cs.isImplicit = false;
3231+
cs.isBasicBasic = cs.isBasicBearer = false;
3232+
cs.scheme = securityScheme.getScheme();
32313233

32323234
if (SecurityScheme.Type.APIKEY.equals(securityScheme.getType())) {
32333235
cs.isBasic = cs.isOAuth = false;
@@ -3239,6 +3241,12 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> securitySc
32393241
} else if (SecurityScheme.Type.HTTP.equals(securityScheme.getType())) {
32403242
cs.isKeyInHeader = cs.isKeyInQuery = cs.isKeyInCookie = cs.isApiKey = cs.isOAuth = false;
32413243
cs.isBasic = true;
3244+
if ("basic".equals(securityScheme.getScheme())) {
3245+
cs.isBasicBasic = true;
3246+
}
3247+
else if ("bearer".equals(securityScheme.getScheme())) {
3248+
cs.isBasicBearer = true;
3249+
}
32423250
} else if (SecurityScheme.Type.OAUTH2.equals(securityScheme.getType())) {
32433251
cs.isKeyInHeader = cs.isKeyInQuery = cs.isKeyInCookie = cs.isApiKey = cs.isBasic = false;
32443252
cs.isOAuth = true;

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java

Lines changed: 201 additions & 15 deletions
Large diffs are not rendered by default.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openapitools.codegen.languages;
17+
18+
import org.openapitools.codegen.*;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import java.io.File;
22+
import java.util.*;
23+
24+
public class PythonAiohttpConnexionServerCodegen extends PythonAbstractConnexionServerCodegen {
25+
private static final Logger LOGGER = LoggerFactory.getLogger(PythonAiohttpConnexionServerCodegen.class);
26+
27+
public PythonAiohttpConnexionServerCodegen() {
28+
super("python-aiohttp", true);
29+
testPackage = "tests";
30+
embeddedTemplateDir = templateDir = "python-aiohttp";
31+
}
32+
33+
/**
34+
* Configures a friendly name for the generator. This will be used by the generator
35+
* to select the library with the -g flag.
36+
*
37+
* @return the friendly name for the generator
38+
*/
39+
@Override
40+
public String getName() {
41+
return "python-aiohttp";
42+
}
43+
44+
@Override
45+
protected void addSupportingFiles() {
46+
supportingFiles.add(new SupportingFile("conftest.mustache", testPackage, "conftest.py"));
47+
supportingFiles.add(new SupportingFile("__init__test.mustache", testPackage, "__init__.py"));
48+
}
49+
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonFlaskConnexionServerCodegen.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package org.openapitools.codegen.languages;
1818

19+
import org.openapitools.codegen.*;
1920
import org.slf4j.Logger;
2021
import org.slf4j.LoggerFactory;
22+
import java.io.File;
2123
import java.util.*;
2224

2325
import static org.openapitools.codegen.utils.StringUtils.camelize;
@@ -27,7 +29,7 @@ public class PythonFlaskConnexionServerCodegen extends PythonAbstractConnexionSe
2729
private static final Logger LOGGER = LoggerFactory.getLogger(PythonFlaskConnexionServerCodegen.class);
2830

2931
public PythonFlaskConnexionServerCodegen() {
30-
super("flaskConnexion");
32+
super("flaskConnexion", false);
3133
}
3234

3335
/**
@@ -40,4 +42,19 @@ public PythonFlaskConnexionServerCodegen() {
4042
public String getName() {
4143
return "python-flask";
4244
}
45+
46+
@Override
47+
protected void addSupportingFiles() {
48+
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
49+
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
50+
supportingFiles.add(new SupportingFile("dockerignore.mustache", "", ".dockerignore"));
51+
supportingFiles.add(new SupportingFile("setup.mustache", "", "setup.py"));
52+
supportingFiles.add(new SupportingFile("tox.mustache", "", "tox.ini"));
53+
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
54+
supportingFiles.add(new SupportingFile("travis.mustache", "", ".travis.yml"));
55+
supportingFiles.add(new SupportingFile("encoder.mustache", packageName, "encoder.py"));
56+
supportingFiles.add(new SupportingFile("__init__test.mustache", packageName + File.separatorChar + testPackage, "__init__.py"));
57+
supportingFiles.add(new SupportingFile("__init__.mustache", packageName, "__init__.py"));
58+
testPackage = packageName + "." + testPackage;
59+
}
4360
}

modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ org.openapitools.codegen.languages.PhpZendExpressivePathHandlerServerCodegen
7474
org.openapitools.codegen.languages.PowerShellClientCodegen
7575
org.openapitools.codegen.languages.PythonClientCodegen
7676
org.openapitools.codegen.languages.PythonFlaskConnexionServerCodegen
77+
org.openapitools.codegen.languages.PythonAiohttpConnexionServerCodegen
7778
org.openapitools.codegen.languages.RClientCodegen
7879
org.openapitools.codegen.languages.RubyClientCodegen
7980
org.openapitools.codegen.languages.RubyOnRailsServerCodegen

modules/openapi-generator/src/main/resources/flaskConnexion/controller_test.mustache

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
22

33
from __future__ import absolute_import
4+
import unittest
45

56
from flask import json
67
from six import BytesIO
@@ -13,7 +14,10 @@ from {{packageName}}.test import BaseTestCase
1314
class {{#operations}}Test{{classname}}(BaseTestCase):
1415
"""{{classname}} integration test stubs"""
1516

16-
{{#operation}}
17+
{{#operation}}
18+
{{#vendorExtensions.x-skip-test}}
19+
@unittest.skip("{{reason}}")
20+
{{/vendorExtensions.x-skip-test}}
1721
def test_{{operationId}}(self):
1822
"""Test case for {{{operationId}}}
1923

@@ -25,18 +29,21 @@ class {{#operations}}Test{{classname}}(BaseTestCase):
2529
{{#queryParams}}
2630
{{#-first}}query_string = [{{/-first}}{{^-first}} {{/-first}}('{{paramName}}', {{{example}}}){{#hasMore}},{{/hasMore}}{{#-last}}]{{/-last}}
2731
{{/queryParams}}
28-
{{#headerParams}}
29-
{{#-first}}headers = [{{/-first}}{{^-first}} {{/-first}}('{{paramName}}', {{{example}}}){{#hasMore}},{{/hasMore}}{{#-last}}]{{/-last}}
30-
{{/headerParams}}
32+
headers = { {{#vendorExtensions.x-prefered-produce}}
33+
'Accept': '{{mediaType}}',{{/vendorExtensions.x-prefered-produce}}{{#vendorExtensions.x-prefered-consume}}
34+
'Content-Type': '{{mediaType}}',{{/vendorExtensions.x-prefered-consume}}{{#headerParams}}
35+
'{{paramName}}': {{{example}}},{{/headerParams}}{{#authMethods}}
36+
{{#isOAuth}}'Authorization': 'Bearer special-key',{{/isOAuth}}{{#isApiKey}}'{{name}}': 'special-key',{{/isApiKey}}{{#isBasic}}'Authorization': '{{#isBasicBasic}}{{#isBasicBasic}} BasicZm9vOmJhcg=={{#isBasicBearer}}Bearer special-key{{#isBasicBearer}}',{{/isBasic}}{{/authMethods}}
37+
}
3138
{{#formParams}}
3239
{{#-first}}data = dict({{/-first}}{{^-first}} {{/-first}}{{paramName}}={{{example}}}{{#hasMore}},{{/hasMore}}{{#-last}}){{/-last}}
3340
{{/formParams}}
3441
response = self.client.open(
3542
'{{#contextPath}}{{{.}}}{{/contextPath}}{{{path}}}'{{#pathParams}}{{#-first}}.format({{/-first}}{{paramName}}={{{example}}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/pathParams}},
36-
method='{{httpMethod}}'{{#bodyParam}},
43+
method='{{httpMethod}}',
44+
headers=headers{{#bodyParam}},
3745
data=json.dumps({{paramName}}){{^consumes}},
38-
content_type='application/json'{{/consumes}}{{/bodyParam}}{{#headerParams}}{{#-first}},
39-
headers=headers{{/-first}}{{/headerParams}}{{#formParams}}{{#-first}},
46+
content_type='application/json'{{/consumes}}{{/bodyParam}}{{#formParams}}{{#-first}},
4047
data=data{{/-first}}{{/formParams}}{{#consumes}}{{#-first}},
4148
content_type='{{{mediaType}}}'{{/-first}}{{/consumes}}{{#queryParams}}{{#-first}},
4249
query_string=query_string{{/-first}}{{/queryParams}})
@@ -47,5 +54,4 @@ class {{#operations}}Test{{classname}}(BaseTestCase):
4754
{{/operations}}
4855

4956
if __name__ == '__main__':
50-
import unittest
5157
unittest.main()

0 commit comments

Comments
 (0)