[python-nextgen] fix issue on API example doc autogeneration (#14539)

* [python-nextgen] fix template to make auto-generated example runnable when spec has no auth methods

* update samples with ./bin/generate-samples.sh

* add unit test code
This commit is contained in:
itaru2622 2023-01-28 13:03:17 +09:00 committed by GitHub
parent 6a2d8d23da
commit 4ffdadfbcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 94 additions and 58 deletions

View File

@ -7,12 +7,7 @@ from {{{packageName}}}.rest import ApiException
from pprint import pprint
{{> python_doc_auth_partial}}
# Enter a context with an instance of the API client
{{#hasAuthMethods}}
{{#asyncio}}async {{/asyncio}}with {{{packageName}}}.ApiClient(configuration) as api_client:
{{/hasAuthMethods}}
{{^hasAuthMethods}}
{{#asyncio}}async {{/asyncio}}with {{{packageName}}}.ApiClient() as api_client:
{{/hasAuthMethods}}
# Create an instance of the API class
api_instance = {{{packageName}}}.{{{classname}}}(api_client)
{{#allParams}}

View File

@ -18,17 +18,28 @@
package org.openapitools.codegen.python;
import com.google.common.collect.Sets;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.PythonNextgenClientCodegen;
import org.openapitools.codegen.languages.features.CXFServerFeatures;
import static org.openapitools.codegen.TestUtils.assertFileContains;
import static org.openapitools.codegen.TestUtils.assertFileExists;
import org.openapitools.codegen.TestUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PythonNextgenClientCodegenTest {
@ -382,4 +393,34 @@ public class PythonNextgenClientCodegenTest {
Assert.assertEquals(cm.parent, null);
Assert.assertEquals(cm.imports.size(), 0);
}
@Test(description ="check API example has input param(configuration) when it creates api_client")
public void apiExampleDocTest() throws Exception {
final DefaultCodegen codegen = new PythonNextgenClientCodegen();
final String outputPath = generateFiles(codegen, "src/test/resources/3_0/generic.yaml");
final Path p = Paths.get(outputPath + "docs/DefaultApi.md");
assertFileExists(p);
assertFileContains(p, "openapi_client.ApiClient(configuration) as api_client");
}
// Helper function, intended to reduce boilerplate
static private String generateFiles(DefaultCodegen codegen, String filePath) throws IOException {
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
final String outputPath = output.getAbsolutePath().replace('\\', '/');
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
final ClientOptInput input = new ClientOptInput();
final OpenAPI openAPI = new OpenAPIParser().readLocation(filePath, null, new ParseOptions()).getOpenAPI();
input.openAPI(openAPI);
input.config(codegen);
final DefaultGenerator generator = new DefaultGenerator();
final List<File> files = generator.opts(input).generate();
Assert.assertTrue(files.size() > 0);
return outputPath + "/";
}
}

View File

@ -31,7 +31,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.AnotherFakeApi(api_client)
client = petstore_api.Client() # Client | client model

View File

@ -29,7 +29,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.DefaultApi(api_client)

View File

@ -44,7 +44,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
@ -232,7 +232,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
body = True # bool | Input boolean as post body (optional)
@ -295,7 +295,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
outer_composite = petstore_api.OuterComposite() # OuterComposite | Input composite as post body (optional)
@ -358,7 +358,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
body = 3.4 # float | Input number as post body (optional)
@ -421,7 +421,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
body = 'body_example' # str | Input string as post body (optional)
@ -484,7 +484,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
outer_object_with_enum_property = petstore_api.OuterObjectWithEnumProperty() # OuterObjectWithEnumProperty | Input enum (int) as post body
@ -547,7 +547,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
body = 'body_example' # str | image to upload
@ -608,7 +608,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
file_schema_test_class = petstore_api.FileSchemaTestClass() # FileSchemaTestClass |
@ -667,7 +667,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
query = 'query_example' # str |
@ -730,7 +730,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
client = petstore_api.Client() # Client | client model
@ -976,7 +976,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
request_body = {'key': 'request_body_example'} # Dict[str, str] | request body
@ -1038,7 +1038,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
param = 'param_example' # str | field1
@ -1102,7 +1102,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
pipe = ['pipe_example'] # List[str] |

View File

@ -34,7 +34,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.StoreApi(api_client)
order_id = 'order_id_example' # str | ID of the order that needs to be deleted
@ -168,7 +168,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.StoreApi(api_client)
order_id = 56 # int | ID of pet that needs to be fetched
@ -234,7 +234,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.StoreApi(api_client)
order = petstore_api.Order() # Order | order placed for purchasing the pet

View File

@ -38,7 +38,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
user = petstore_api.User() # User | Created user object
@ -100,7 +100,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
user = [petstore_api.User()] # List[User] | List of user object
@ -162,7 +162,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
user = [petstore_api.User()] # List[User] | List of user object
@ -224,7 +224,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
username = 'username_example' # str | The name that needs to be deleted
@ -287,7 +287,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
username = 'username_example' # str | The name that needs to be fetched. Use user1 for testing.
@ -353,7 +353,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
username = 'username_example' # str | The user name for login
@ -420,7 +420,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
@ -478,7 +478,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
async with petstore_api.ApiClient() as api_client:
async with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
username = 'username_example' # str | name that need to be deleted

View File

@ -31,7 +31,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.AnotherFakeApi(api_client)
client = petstore_api.Client() # Client | client model

View File

@ -29,7 +29,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.DefaultApi(api_client)

View File

@ -44,7 +44,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
@ -232,7 +232,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
body = True # bool | Input boolean as post body (optional)
@ -295,7 +295,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
outer_composite = petstore_api.OuterComposite() # OuterComposite | Input composite as post body (optional)
@ -358,7 +358,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
body = 3.4 # float | Input number as post body (optional)
@ -421,7 +421,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
body = 'body_example' # str | Input string as post body (optional)
@ -484,7 +484,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
outer_object_with_enum_property = petstore_api.OuterObjectWithEnumProperty() # OuterObjectWithEnumProperty | Input enum (int) as post body
@ -547,7 +547,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
body = 'body_example' # str | image to upload
@ -608,7 +608,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
file_schema_test_class = petstore_api.FileSchemaTestClass() # FileSchemaTestClass |
@ -667,7 +667,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
query = 'query_example' # str |
@ -730,7 +730,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
client = petstore_api.Client() # Client | client model
@ -976,7 +976,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
request_body = {'key': 'request_body_example'} # Dict[str, str] | request body
@ -1038,7 +1038,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
param = 'param_example' # str | field1
@ -1102,7 +1102,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
pipe = ['pipe_example'] # List[str] |

View File

@ -34,7 +34,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.StoreApi(api_client)
order_id = 'order_id_example' # str | ID of the order that needs to be deleted
@ -168,7 +168,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.StoreApi(api_client)
order_id = 56 # int | ID of pet that needs to be fetched
@ -234,7 +234,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.StoreApi(api_client)
order = petstore_api.Order() # Order | order placed for purchasing the pet

View File

@ -38,7 +38,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
user = petstore_api.User() # User | Created user object
@ -100,7 +100,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
user = [petstore_api.User()] # List[User] | List of user object
@ -162,7 +162,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
user = [petstore_api.User()] # List[User] | List of user object
@ -224,7 +224,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
username = 'username_example' # str | The name that needs to be deleted
@ -287,7 +287,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
username = 'username_example' # str | The name that needs to be fetched. Use user1 for testing.
@ -353,7 +353,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
username = 'username_example' # str | The user name for login
@ -420,7 +420,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
@ -478,7 +478,7 @@ configuration = petstore_api.Configuration(
# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.UserApi(api_client)
username = 'username_example' # str | name that need to be deleted