Re-implementation of Apex client code gen (#698)

This commit is contained in:
René Winkelmeyer
2018-07-31 17:02:25 +02:00
committed by William Cheng
parent f1897c4462
commit d1fc923b66
135 changed files with 980 additions and 5882 deletions

View File

@@ -393,7 +393,7 @@ public abstract class AbstractApexCodegen extends DefaultCodegen implements Code
} else if (ModelUtils.isObjectSchema(p)) {
example = example.isEmpty() ? "null" : example;
} else {
example = super.toExampleValue(p);
example = getTypeDeclaration(p) + ".getExample()";
}
return example;
}
@@ -462,7 +462,7 @@ public abstract class AbstractApexCodegen extends DefaultCodegen implements Code
cm.vendorExtensions.put("propertyMappings", propertyMappings);
if (!propertyMappings.isEmpty()) {
cm.interfaces.add("Swagger.MappedProperties");
cm.interfaces.add("OAS.MappedProperties");
}
return cm;
}
@@ -565,24 +565,6 @@ public abstract class AbstractApexCodegen extends DefaultCodegen implements Code
@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Schema> definitions, OpenAPI openAPI) {
/* TODO the following logic revised. Maybe we should simply use the consumes, produces provided by the spec
Boolean hasFormParams = false;
for (Parameter p : operation.getParameters()) {
if ("formData".equals(p.getIn())) {
hasFormParams = true;
break;
}
}
// only support serialization into JSON and urlencoded forms for now
operation.setConsumes(
Collections.singletonList(hasFormParams
? "application/x-www-form-urlencoded"
: "application/json"));
// only support deserialization from JSON for now
operation.setProduces(Collections.singletonList("application/json"));
*/
CodegenOperation op = super.fromOperation(
path, httpMethod, operation, definitions, openAPI);

View File

@@ -36,7 +36,7 @@ public class ApexClientCodegen extends AbstractApexCodegen {
private static final String BUILD_METHOD = "buildMethod";
private static final String NAMED_CREDENTIAL = "namedCredential";
private static final Logger LOGGER = LoggerFactory.getLogger(ApexClientCodegen.class);
private String classPrefix = "Swag";
private String classPrefix = "OAS";
private String apiVersion = "42.0";
private String buildMethod = "sfdx";
private String namedCredential = classPrefix;
@@ -69,12 +69,12 @@ public class ApexClientCodegen extends AbstractApexCodegen {
cliOptions.add(CliOption.newString(BUILD_METHOD, "The build method for this package."));
cliOptions.add(CliOption.newString(NAMED_CREDENTIAL, "The named credential name for the HTTP callouts"));
supportingFiles.add(new SupportingFile("Swagger.cls", srcPath + "classes", "Swagger.cls"));
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "Swagger.cls-meta.xml"));
supportingFiles.add(new SupportingFile("SwaggerTest.cls", srcPath + "classes", "SwaggerTest.cls"));
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "SwaggerTest.cls-meta.xml"));
supportingFiles.add(new SupportingFile("SwaggerResponseMock.cls", srcPath + "classes", "SwaggerResponseMock.cls"));
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "SwaggerResponseMock.cls-meta.xml"));
supportingFiles.add(new SupportingFile("OAS.cls", srcPath + "classes", "OAS.cls"));
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "OAS.cls-meta.xml"));
supportingFiles.add(new SupportingFile("OASTest.cls", srcPath + "classes", "OASTest.cls"));
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "OASTest.cls-meta.xml"));
supportingFiles.add(new SupportingFile("OASResponseMock.cls", srcPath + "classes", "OASResponseMock.cls"));
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "OASResponseMock.cls-meta.xml"));
typeMapping.put("BigDecimal", "Double");
typeMapping.put("binary", "String");
@@ -248,8 +248,6 @@ public class ApexClientCodegen extends AbstractApexCodegen {
public void setBuildMethod(String buildMethod) {
if (buildMethod.equals("ant")) {
this.srcPath = "deploy/";
} else {
this.srcPath = "src/";
}
this.buildMethod = buildMethod;
}

View File

@@ -1,4 +1,4 @@
public class Swagger {
public class OAS {
private static final String HEADER_CONTENT_TYPE = 'Content-Type';
private static final String HEADER_ACCEPT = 'Accept';
private static final String HEADER_ACCEPT_DELIMITER = ',';

View File

@@ -1,9 +1,9 @@
@isTest
public class SwaggerResponseMock implements HttpCalloutMock {
public class OASResponseMock implements HttpCalloutMock {
private final HttpResponse response;
private HttpRequest request;
public SwaggerResponseMock(HttpResponse response) {
public OASResponseMock(HttpResponse response) {
this.response = response;
}

View File

@@ -1,19 +1,19 @@
@isTest
private class SwaggerTest {
private class OASTest {
@isTest
private static void Param_urlEncodeKeyValuePairUtf8() {
String toEncodeLeft = 'Hello +%-_.!~*\'()@';
String toEncodeRight = 'World +%-_.!~*\'()@';
String expected = 'Hello+%2B%25-_.%21%7E*%27%28%29%40=World+%2B%25-_.%21%7E*%27%28%29%40';
String result = new Swagger.Param(toEncodeLeft, toEncodeRight).toString();
String result = new OAS.Param(toEncodeLeft, toEncodeRight).toString();
System.assertEquals(expected, result);
}
@isTest
private static void ApiKeyHeaderAuth_keyInHeaderWithGivenName() {
Map<String, Object> headers = new Map<String, String>();
List<Swagger.Param> query = new List<Swagger.Param>();
Swagger.ApiKeyHeaderAuth auth = new Swagger.ApiKeyHeaderAuth('X-Authenticate');
List<OAS.Param> query = new List<OAS.Param>();
OAS.ApiKeyHeaderAuth auth = new OAS.ApiKeyHeaderAuth('X-Authenticate');
auth.setApiKey('foo-bar-api-key');
auth.apply(headers, query);
@@ -25,8 +25,8 @@ private class SwaggerTest {
@isTest
private static void ApiKeyQueryAuth_keyInQueryParamWithGivenName() {
Map<String, Object> headers = new Map<String, String>();
List<Swagger.Param> query = new List<Swagger.Param>();
Swagger.ApiKeyQueryAuth auth = new Swagger.ApiKeyQueryAuth('auth_token');
List<OAS.Param> query = new List<OAS.Param>();
OAS.ApiKeyQueryAuth auth = new OAS.ApiKeyQueryAuth('auth_token');
auth.setApiKey('foo-bar-api-key');
auth.apply(headers, query);
@@ -38,8 +38,8 @@ private class SwaggerTest {
@isTest
private static void ApiClient_returnAuthenticationMatchingInput() {
MockApiClient client = new MockApiClient();
Swagger.ApiKeyHeaderAuth auth1 = new Swagger.ApiKeyHeaderAuth('foo');
Swagger.ApiKeyQueryAuth auth2 = new Swagger.ApiKeyQueryAuth('foo');
OAS.ApiKeyHeaderAuth auth1 = new OAS.ApiKeyHeaderAuth('foo');
OAS.ApiKeyQueryAuth auth2 = new OAS.ApiKeyQueryAuth('foo');
client.authentications.put('auth1', auth1);
client.authentications.put('auth2', auth2);
@@ -51,8 +51,8 @@ private class SwaggerTest {
@isTest
private static void ApiClient_oneKeyValuePairForEachValueInList() {
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
Swagger.ApiClient client = new Swagger.ApiClient();
List<Swagger.Param> params = client.makeParams('foo', values);
OAS.ApiClient client = new OAS.ApiClient();
List<OAS.Param> params = client.makeParams('foo', values);
System.assertEquals(5, params.size());
System.assertEquals('foo=bar', params.get(0).toString());
@@ -64,8 +64,8 @@ private class SwaggerTest {
@isTest
private static void ApiClient_nullMultiValuesListToEmptyParamsList() {
Swagger.ApiClient client = new Swagger.ApiClient();
List<Swagger.Param> params = client.makeParams('foo', null);
OAS.ApiClient client = new OAS.ApiClient();
List<OAS.Param> params = client.makeParams('foo', null);
System.assert(params.isEmpty());
}
@@ -73,8 +73,8 @@ private class SwaggerTest {
@isTest
private static void ApiClient_valuesListToSingleCsvKeyValuePair() {
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
Swagger.ApiClient client = new Swagger.ApiClient();
List<Swagger.Param> params = client.makeParam('foo', values, 'csv');
OAS.ApiClient client = new OAS.ApiClient();
List<OAS.Param> params = client.makeParam('foo', values, 'csv');
System.assertEquals(1, params.size());
System.assertEquals('foo=bar%2C4%2Cfalse%2C12.4%2C', params.get(0).toString());
@@ -83,8 +83,8 @@ private class SwaggerTest {
@isTest
private static void ApiClient_valuesListToSingleSsvKeyValuePair() {
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
Swagger.ApiClient client = new Swagger.ApiClient();
List<Swagger.Param> params = client.makeParam('foo', values, 'ssv');
OAS.ApiClient client = new OAS.ApiClient();
List<OAS.Param> params = client.makeParam('foo', values, 'ssv');
System.assertEquals(1, params.size());
System.assertEquals('foo=bar+4+false+12.4+', params.get(0).toString());
@@ -93,8 +93,8 @@ private class SwaggerTest {
@isTest
private static void ApiClient_valuesListToSingleTsvKeyValuePair() {
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
Swagger.ApiClient client = new Swagger.ApiClient();
List<Swagger.Param> params = client.makeParam('foo', values, 'tsv');
OAS.ApiClient client = new OAS.ApiClient();
List<OAS.Param> params = client.makeParam('foo', values, 'tsv');
System.assertEquals(1, params.size());
System.assertEquals('foo=bar%094%09false%0912.4%09', params.get(0).toString());
@@ -103,8 +103,8 @@ private class SwaggerTest {
@isTest
private static void ApiClient_valuesListToSinglePipeSeparatedKeyValuePair() {
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
Swagger.ApiClient client = new Swagger.ApiClient();
List<Swagger.Param> params = client.makeParam('foo', values, 'pipes');
OAS.ApiClient client = new OAS.ApiClient();
List<OAS.Param> params = client.makeParam('foo', values, 'pipes');
System.assertEquals(1, params.size());
System.assertEquals('foo=bar%7C4%7Cfalse%7C12.4%7C', params.get(0).toString());
@@ -112,16 +112,16 @@ private class SwaggerTest {
@isTest
private static void ApiClient_nullValuesListToEmptyParamsList() {
Swagger.ApiClient client = new Swagger.ApiClient();
List<Swagger.Param> params = client.makeParam('foo', null, 'csv');
OAS.ApiClient client = new OAS.ApiClient();
List<OAS.Param> params = client.makeParam('foo', null, 'csv');
System.assert(params.isEmpty());
}
@isTest
private static void ApiClient_paramsFromAnyPrimitiveTypeDiscardNull() {
Swagger.ApiClient client = new Swagger.ApiClient();
List<Swagger.Param> params = new List<Swagger.Param>();
OAS.ApiClient client = new OAS.ApiClient();
List<OAS.Param> params = new List<OAS.Param>();
params.addAll(client.makeParam('foo', 'bar'));
params.addAll(client.makeParam('foo', 10));
params.addAll(client.makeParam('foo', 12.6));
@@ -141,13 +141,13 @@ private class SwaggerTest {
@isTest
private static void ApiClient_requiredParameterPasses() {
Swagger.ApiClient client = new Swagger.ApiClient();
OAS.ApiClient client = new OAS.ApiClient();
client.assertNotNull('foo', 'bar');
}
@isTest
private static void ApiClient_requiredParameterFails() {
Swagger.ApiClient client = new Swagger.ApiClient();
OAS.ApiClient client = new OAS.ApiClient();
try {
client.assertNotNull(null, 'bar');
} catch (NullPointerException e) {
@@ -217,7 +217,7 @@ private class SwaggerTest {
'"bat":false'
};
Set<String> actual1 = new Set<String>(client
.toBody('application/json', body1, new List<Swagger.Param>())
.toBody('application/json', body1, new List<OAS.Param>())
.removeStart('{')
.removeEnd('}')
.split(',')
@@ -225,12 +225,12 @@ private class SwaggerTest {
System.assertEquals(expected1, actual1);
String body2 = 'Hello, World!';
String actual2 = client.toBody('text/plain', body2, new List<Swagger.Param>());
String actual2 = client.toBody('text/plain', body2, new List<OAS.Param>());
System.assertEquals(body2, actual2);
List<Swagger.Param> form = new List<Swagger.Param>{
new Swagger.Param('hello', 'world'),
new Swagger.Param('date', '2017-01-01 15:00:00')
List<OAS.Param> form = new List<OAS.Param>{
new OAS.Param('hello', 'world'),
new OAS.Param('date', '2017-01-01 15:00:00')
};
String expected3 = 'hello=world&date=2017-01-01+15%3A00%3A00';
String actual3 = client.toBody('application/x-www-form-urlencoded', '', form);
@@ -288,9 +288,9 @@ private class SwaggerTest {
MockApiClient client = new MockApiClient();
String path = '/departments/{department}';
Map<String, Object> params = new Map<String, Object>{'department' => 'finance'};
List<Swagger.Param> queryParams = new List<Swagger.Param>{
new Swagger.Param('foo', 'bar'),
new Swagger.Param('bat', '123')
List<OAS.Param> queryParams = new List<OAS.Param>{
new OAS.Param('foo', 'bar'),
new OAS.Param('bat', '123')
};
String expected = 'callout:Winkelmeyer/departments/finance?foo=bar&bat=123';
String actual = client.toEndpoint(path, params, queryParams);
@@ -301,7 +301,7 @@ private class SwaggerTest {
private static void ApiClient_returnParsedBody() {
MockApiClient client = new MockApiClient();
HttpResponse res = new HttpResponse();
SwaggerResponseMock mock = new SwaggerResponseMock(res);
OASResponseMock mock = new OASResponseMock(res);
Test.setMock(HttpCalloutMock.class, mock);
res.setStatus('OK');
@@ -314,8 +314,8 @@ private class SwaggerTest {
Address a = (Address) client.invoke(
'GET', '/address', '',
new List<Swagger.Param>(),
new List<Swagger.Param>(),
new List<OAS.Param>(),
new List<OAS.Param>(),
new Map<String, Object>(),
new Map<String, Object>(),
new List<String>{'application/json'},
@@ -337,7 +337,7 @@ private class SwaggerTest {
private static void ApiClient_noReturnTypeReturnsNull() {
MockApiClient client = new MockApiClient();
HttpResponse res = new HttpResponse();
SwaggerResponseMock mock = new SwaggerResponseMock(res);
OASResponseMock mock = new OASResponseMock(res);
Test.setMock(HttpCalloutMock.class, mock);
res.setStatus('OK');
@@ -345,8 +345,8 @@ private class SwaggerTest {
Object o = client.invoke(
'POST', '/address', '',
new List<Swagger.Param>(),
new List<Swagger.Param>(),
new List<OAS.Param>(),
new List<OAS.Param>(),
new Map<String, Object>(),
new Map<String, Object>(),
new List<String>{'application/json'},
@@ -358,7 +358,7 @@ private class SwaggerTest {
System.assertEquals(null, o);
}
private class MockApiClient extends Swagger.ApiClient {
private class MockApiClient extends OAS.ApiClient {
public MockApiClient() {
basePath = 'https://blog.winkelmeyer.com';
calloutName = 'Winkelmeyer';

View File

@@ -116,7 +116,7 @@ HttpBasicAuth {{{name}}} = (HttpBasicAuth) client.getAuthentication('{{{name}}}'
ApiKeyAuth {{{name}}} = (ApiKeyAuth) client.getAuthentication('{{{name}}}');
{{{name}}}.setApiKey('YOUR API KEY');{{/isApiKey}}{{#isOAuth}}
// Configure OAuth2 access token for authorization: {{{name}}}
Swagger.OAuth {{{name}}} = (Swagger.OAuth) client.getAuthentication('{{{name}}}');
OAS.OAuth {{{name}}} = (OAS.OAuth) client.getAuthentication('{{{name}}}');
{{{name}}}.setAccessToken('YOUR ACCESS TOKEN');{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
@@ -135,7 +135,7 @@ try {
{{#returnType}}
System.debug(result);
{{/returnType}}
} catch (Swagger.ApiException e) {
} catch (OAS.ApiException e) {
// ...handle your exceptions
}{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
```

View File

@@ -67,7 +67,7 @@ try {
{{#returnType}}
System.debug(result);
{{/returnType}}
} catch (Swagger.ApiException e) {
} catch (OAS.ApiException e) {
// ...handle your exceptions
}{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
```

View File

@@ -25,7 +25,7 @@ public class {{classname}} {
{{#returnType}}
* @return {{{returnType}}}
{{/returnType}}
* @throws Swagger.ApiException if fails to make API call
* @throws OAS.ApiException if fails to make API call
*/
public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}}({{#hasParams}}Map<String, Object> params{{/hasParams}}) {
{{#allParams}}
@@ -33,7 +33,7 @@ public class {{classname}} {
client.assertNotNull(params.get('{{paramName}}'), '{{paramName}}');
{{/required}}
{{/allParams}}
List<Swagger.Param> query = new List<Swagger.Param>();
List<OAS.Param> query = new List<OAS.Param>();
{{#hasQueryParams}}
// cast query params to verify their expected type
@@ -54,7 +54,7 @@ public class {{classname}} {
{{/hasMore}}
{{/queryParams}}
List<Swagger.Param> form = new List<Swagger.Param>();
List<OAS.Param> form = new List<OAS.Param>();
{{#hasFormParams}}
// cast form params to verify their expected type

View File

@@ -35,7 +35,7 @@ HttpBasicAuth {{{name}}} = (HttpBasicAuth) client.getAuthentication('{{{name}}}'
ApiKeyAuth {{{name}}} = (ApiKeyAuth) client.getAuthentication('{{{name}}}');
{{{name}}}.setApiKey('YOUR API KEY');{{/isApiKey}}{{#isOAuth}}
// Configure OAuth2 access token for authorization: {{{name}}}
Swagger.OAuth {{{name}}} = (Swagger.OAuth) client.getAuthentication('{{{name}}}');
OAS.OAuth {{{name}}} = (OAS.OAuth) client.getAuthentication('{{{name}}}');
{{{name}}}.setAccessToken('YOUR ACCESS TOKEN');{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
@@ -54,7 +54,7 @@ try {
{{#returnType}}
System.debug(result);
{{/returnType}}
} catch (Swagger.ApiException e) {
} catch (OAS.ApiException e) {
// ...handle your exceptions
}
```

View File

@@ -18,7 +18,7 @@ private class {{classname}}Test {
res.setStatusCode(200);
res.setStatus('OK');
{{/restfulCreate}}
Test.setMock(HttpCalloutMock.class, new SwaggerResponseMock(res));
Test.setMock(HttpCalloutMock.class, new OASResponseMock(res));
{{#hasParams}}
Map<String, Object> params = new Map<String, Object>{
@@ -39,7 +39,7 @@ private class {{classname}}Test {
{{#authMethods}}
client = new {{classPrefix}}Client();
api = new {{classname}}(client);{{#isApiKey}}
((Swagger.ApiKeyAuth)client.getAuthentication('{{name}}')).setApiKey('foo-bar-api-key');
((OAS.ApiKeyAuth)client.getAuthentication('{{name}}')).setApiKey('foo-bar-api-key');
{{/isApiKey}}
{{#examples}}

View File

@@ -62,7 +62,7 @@
<runTest>{{classname}}Test</runTest>
{{/model}}
{{/models}}
<runTest>SwaggerTest</runTest>
<runTest>OASTest</runTest>
</sf:deploy>
</target>
@@ -92,7 +92,7 @@
<runTest>{{classname}}Test</runTest>
{{/model}}
{{/models}}
<runTest>SwaggerTest</runTest>
<runTest>OASTest</runTest>
</sf:deploy>
</target>
</project>

View File

@@ -1,4 +1,4 @@
public class {{classPrefix}}Client extends Swagger.ApiClient {
public class {{classPrefix}}Client extends OAS.ApiClient {
{{#hasAuthMethods}}
public {{classPrefix}}Client() {
basePath = '{{basePath}}';
@@ -6,10 +6,10 @@ public class {{classPrefix}}Client extends Swagger.ApiClient {
{{#authMethods}}
{{#isApiKey}}
{{#isKeyInQuery}}
authentications.put('{{name}}', new Swagger.ApiKeyQueryAuth('{{keyParamName}}'));
authentications.put('{{name}}', new OAS.ApiKeyQueryAuth('{{keyParamName}}'));
{{/isKeyInQuery}}
{{^isKeyInQuery}}
authentications.put('{{name}}', new Swagger.ApiKeyHeaderAuth('{{keyParamName}}'));
authentications.put('{{name}}', new OAS.ApiKeyHeaderAuth('{{keyParamName}}'));
{{/isKeyInQuery}}
{{/isApiKey}}
{{/authMethods}}

View File

@@ -1,7 +1,7 @@
#!/bin/sh
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
#
# Usage example: /bin/sh ./git_push.sh wing328 swagger-petstore-perl "minor update"
# Usage example: /bin/sh ./git_push.sh wing328 OAS-petstore-perl "minor update"
git_user_id=$1
git_repo_id=$2

View File

@@ -5,7 +5,7 @@
* {{#version}}OpenAPI spec version: {{{version}}}{{/version}}
* {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* NOTE: This class is auto generated by the OAS code generator program.
* https://github.com/OAS-api/OAS-codegen.git
* Do not edit the class manually.
*/

View File

@@ -3,7 +3,7 @@
<fullName>{{appName}} API Client</fullName>
<description>Client library for calling the {{appName}} API.{{#appDescription}}
{{{appDescription}}}{{/appDescription}}
Generated with Swagger Codegen (github.com/swagger-api/swagger-codegen)</description>
Generated with OAS Codegen (github.com/OAS-api/OAS-codegen)</description>
<types>
{{#apiInfo}}
{{#apis}}
@@ -18,9 +18,9 @@ Generated with Swagger Codegen (github.com/swagger-api/swagger-codegen)</descrip
{{/model}}
{{/models}}
<members>{{classPrefix}}Client</members>
<members>Swagger</members>
<members>SwaggerTest</members>
<members>SwaggerResponseMock</members>
<members>OAS</members>
<members>OASTest</members>
<members>OASResponseMock</members>
<name>ApexClass</name>
</types>
<types>