Skip to content

Commit d1fc923

Browse files
muenzpraegerwing328
authored andcommitted
Re-implementation of Apex client code gen (#698)
1 parent f1897c4 commit d1fc923

135 files changed

Lines changed: 980 additions & 5882 deletions

File tree

Some content is hidden

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

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public String toExampleValue(Schema p) {
393393
} else if (ModelUtils.isObjectSchema(p)) {
394394
example = example.isEmpty() ? "null" : example;
395395
} else {
396-
example = super.toExampleValue(p);
396+
example = getTypeDeclaration(p) + ".getExample()";
397397
}
398398
return example;
399399
}
@@ -462,7 +462,7 @@ public CodegenModel fromModel(String name, Schema model, Map<String, Schema> all
462462
cm.vendorExtensions.put("propertyMappings", propertyMappings);
463463

464464
if (!propertyMappings.isEmpty()) {
465-
cm.interfaces.add("Swagger.MappedProperties");
465+
cm.interfaces.add("OAS.MappedProperties");
466466
}
467467
return cm;
468468
}
@@ -565,24 +565,6 @@ public String toEnumValue(String value, String datatype) {
565565

566566
@Override
567567
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Schema> definitions, OpenAPI openAPI) {
568-
/* TODO the following logic revised. Maybe we should simply use the consumes, produces provided by the spec
569-
Boolean hasFormParams = false;
570-
for (Parameter p : operation.getParameters()) {
571-
if ("formData".equals(p.getIn())) {
572-
hasFormParams = true;
573-
break;
574-
}
575-
}
576-
577-
// only support serialization into JSON and urlencoded forms for now
578-
operation.setConsumes(
579-
Collections.singletonList(hasFormParams
580-
? "application/x-www-form-urlencoded"
581-
: "application/json"));
582-
583-
// only support deserialization from JSON for now
584-
operation.setProduces(Collections.singletonList("application/json"));
585-
*/
586568

587569
CodegenOperation op = super.fromOperation(
588570
path, httpMethod, operation, definitions, openAPI);

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class ApexClientCodegen extends AbstractApexCodegen {
3636
private static final String BUILD_METHOD = "buildMethod";
3737
private static final String NAMED_CREDENTIAL = "namedCredential";
3838
private static final Logger LOGGER = LoggerFactory.getLogger(ApexClientCodegen.class);
39-
private String classPrefix = "Swag";
39+
private String classPrefix = "OAS";
4040
private String apiVersion = "42.0";
4141
private String buildMethod = "sfdx";
4242
private String namedCredential = classPrefix;
@@ -69,12 +69,12 @@ public ApexClientCodegen() {
6969
cliOptions.add(CliOption.newString(BUILD_METHOD, "The build method for this package."));
7070
cliOptions.add(CliOption.newString(NAMED_CREDENTIAL, "The named credential name for the HTTP callouts"));
7171

72-
supportingFiles.add(new SupportingFile("Swagger.cls", srcPath + "classes", "Swagger.cls"));
73-
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "Swagger.cls-meta.xml"));
74-
supportingFiles.add(new SupportingFile("SwaggerTest.cls", srcPath + "classes", "SwaggerTest.cls"));
75-
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "SwaggerTest.cls-meta.xml"));
76-
supportingFiles.add(new SupportingFile("SwaggerResponseMock.cls", srcPath + "classes", "SwaggerResponseMock.cls"));
77-
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "SwaggerResponseMock.cls-meta.xml"));
72+
supportingFiles.add(new SupportingFile("OAS.cls", srcPath + "classes", "OAS.cls"));
73+
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "OAS.cls-meta.xml"));
74+
supportingFiles.add(new SupportingFile("OASTest.cls", srcPath + "classes", "OASTest.cls"));
75+
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "OASTest.cls-meta.xml"));
76+
supportingFiles.add(new SupportingFile("OASResponseMock.cls", srcPath + "classes", "OASResponseMock.cls"));
77+
supportingFiles.add(new SupportingFile("cls-meta.mustache", srcPath + "classes", "OASResponseMock.cls-meta.xml"));
7878

7979
typeMapping.put("BigDecimal", "Double");
8080
typeMapping.put("binary", "String");
@@ -248,8 +248,6 @@ public String toDefaultValue(Schema p) {
248248
public void setBuildMethod(String buildMethod) {
249249
if (buildMethod.equals("ant")) {
250250
this.srcPath = "deploy/";
251-
} else {
252-
this.srcPath = "src/";
253251
}
254252
this.buildMethod = buildMethod;
255253
}

modules/openapi-generator/src/main/resources/apex/Swagger.cls renamed to modules/openapi-generator/src/main/resources/apex/OAS.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class Swagger {
1+
public class OAS {
22
private static final String HEADER_CONTENT_TYPE = 'Content-Type';
33
private static final String HEADER_ACCEPT = 'Accept';
44
private static final String HEADER_ACCEPT_DELIMITER = ',';

samples/client/petstore/apex/force-app/main/default/classes/SwaggerResponseMock.cls renamed to modules/openapi-generator/src/main/resources/apex/OASResponseMock.cls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@isTest
2-
public class SwaggerResponseMock implements HttpCalloutMock {
2+
public class OASResponseMock implements HttpCalloutMock {
33
private final HttpResponse response;
44
private HttpRequest request;
55

6-
public SwaggerResponseMock(HttpResponse response) {
6+
public OASResponseMock(HttpResponse response) {
77
this.response = response;
88
}
99

samples/client/petstore/apex/force-app/main/default/classes/SwaggerTest.cls renamed to modules/openapi-generator/src/main/resources/apex/OASTest.cls

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
@isTest
2-
private class SwaggerTest {
2+
private class OASTest {
33
@isTest
44
private static void Param_urlEncodeKeyValuePairUtf8() {
55
String toEncodeLeft = 'Hello +%-_.!~*\'()@';
66
String toEncodeRight = 'World +%-_.!~*\'()@';
77
String expected = 'Hello+%2B%25-_.%21%7E*%27%28%29%40=World+%2B%25-_.%21%7E*%27%28%29%40';
8-
String result = new Swagger.Param(toEncodeLeft, toEncodeRight).toString();
8+
String result = new OAS.Param(toEncodeLeft, toEncodeRight).toString();
99
System.assertEquals(expected, result);
1010
}
1111

1212
@isTest
1313
private static void ApiKeyHeaderAuth_keyInHeaderWithGivenName() {
1414
Map<String, Object> headers = new Map<String, String>();
15-
List<Swagger.Param> query = new List<Swagger.Param>();
16-
Swagger.ApiKeyHeaderAuth auth = new Swagger.ApiKeyHeaderAuth('X-Authenticate');
15+
List<OAS.Param> query = new List<OAS.Param>();
16+
OAS.ApiKeyHeaderAuth auth = new OAS.ApiKeyHeaderAuth('X-Authenticate');
1717
auth.setApiKey('foo-bar-api-key');
1818
auth.apply(headers, query);
1919

@@ -25,8 +25,8 @@ private class SwaggerTest {
2525
@isTest
2626
private static void ApiKeyQueryAuth_keyInQueryParamWithGivenName() {
2727
Map<String, Object> headers = new Map<String, String>();
28-
List<Swagger.Param> query = new List<Swagger.Param>();
29-
Swagger.ApiKeyQueryAuth auth = new Swagger.ApiKeyQueryAuth('auth_token');
28+
List<OAS.Param> query = new List<OAS.Param>();
29+
OAS.ApiKeyQueryAuth auth = new OAS.ApiKeyQueryAuth('auth_token');
3030
auth.setApiKey('foo-bar-api-key');
3131
auth.apply(headers, query);
3232

@@ -38,8 +38,8 @@ private class SwaggerTest {
3838
@isTest
3939
private static void ApiClient_returnAuthenticationMatchingInput() {
4040
MockApiClient client = new MockApiClient();
41-
Swagger.ApiKeyHeaderAuth auth1 = new Swagger.ApiKeyHeaderAuth('foo');
42-
Swagger.ApiKeyQueryAuth auth2 = new Swagger.ApiKeyQueryAuth('foo');
41+
OAS.ApiKeyHeaderAuth auth1 = new OAS.ApiKeyHeaderAuth('foo');
42+
OAS.ApiKeyQueryAuth auth2 = new OAS.ApiKeyQueryAuth('foo');
4343

4444
client.authentications.put('auth1', auth1);
4545
client.authentications.put('auth2', auth2);
@@ -51,8 +51,8 @@ private class SwaggerTest {
5151
@isTest
5252
private static void ApiClient_oneKeyValuePairForEachValueInList() {
5353
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
54-
Swagger.ApiClient client = new Swagger.ApiClient();
55-
List<Swagger.Param> params = client.makeParams('foo', values);
54+
OAS.ApiClient client = new OAS.ApiClient();
55+
List<OAS.Param> params = client.makeParams('foo', values);
5656

5757
System.assertEquals(5, params.size());
5858
System.assertEquals('foo=bar', params.get(0).toString());
@@ -64,17 +64,17 @@ private class SwaggerTest {
6464

6565
@isTest
6666
private static void ApiClient_nullMultiValuesListToEmptyParamsList() {
67-
Swagger.ApiClient client = new Swagger.ApiClient();
68-
List<Swagger.Param> params = client.makeParams('foo', null);
67+
OAS.ApiClient client = new OAS.ApiClient();
68+
List<OAS.Param> params = client.makeParams('foo', null);
6969

7070
System.assert(params.isEmpty());
7171
}
7272

7373
@isTest
7474
private static void ApiClient_valuesListToSingleCsvKeyValuePair() {
7575
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
76-
Swagger.ApiClient client = new Swagger.ApiClient();
77-
List<Swagger.Param> params = client.makeParam('foo', values, 'csv');
76+
OAS.ApiClient client = new OAS.ApiClient();
77+
List<OAS.Param> params = client.makeParam('foo', values, 'csv');
7878

7979
System.assertEquals(1, params.size());
8080
System.assertEquals('foo=bar%2C4%2Cfalse%2C12.4%2C', params.get(0).toString());
@@ -83,8 +83,8 @@ private class SwaggerTest {
8383
@isTest
8484
private static void ApiClient_valuesListToSingleSsvKeyValuePair() {
8585
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
86-
Swagger.ApiClient client = new Swagger.ApiClient();
87-
List<Swagger.Param> params = client.makeParam('foo', values, 'ssv');
86+
OAS.ApiClient client = new OAS.ApiClient();
87+
List<OAS.Param> params = client.makeParam('foo', values, 'ssv');
8888

8989
System.assertEquals(1, params.size());
9090
System.assertEquals('foo=bar+4+false+12.4+', params.get(0).toString());
@@ -93,8 +93,8 @@ private class SwaggerTest {
9393
@isTest
9494
private static void ApiClient_valuesListToSingleTsvKeyValuePair() {
9595
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
96-
Swagger.ApiClient client = new Swagger.ApiClient();
97-
List<Swagger.Param> params = client.makeParam('foo', values, 'tsv');
96+
OAS.ApiClient client = new OAS.ApiClient();
97+
List<OAS.Param> params = client.makeParam('foo', values, 'tsv');
9898

9999
System.assertEquals(1, params.size());
100100
System.assertEquals('foo=bar%094%09false%0912.4%09', params.get(0).toString());
@@ -103,25 +103,25 @@ private class SwaggerTest {
103103
@isTest
104104
private static void ApiClient_valuesListToSinglePipeSeparatedKeyValuePair() {
105105
List<Object> values = new List<Object>{'bar', 4, false, 12.4, ''};
106-
Swagger.ApiClient client = new Swagger.ApiClient();
107-
List<Swagger.Param> params = client.makeParam('foo', values, 'pipes');
106+
OAS.ApiClient client = new OAS.ApiClient();
107+
List<OAS.Param> params = client.makeParam('foo', values, 'pipes');
108108

109109
System.assertEquals(1, params.size());
110110
System.assertEquals('foo=bar%7C4%7Cfalse%7C12.4%7C', params.get(0).toString());
111111
}
112112

113113
@isTest
114114
private static void ApiClient_nullValuesListToEmptyParamsList() {
115-
Swagger.ApiClient client = new Swagger.ApiClient();
116-
List<Swagger.Param> params = client.makeParam('foo', null, 'csv');
115+
OAS.ApiClient client = new OAS.ApiClient();
116+
List<OAS.Param> params = client.makeParam('foo', null, 'csv');
117117

118118
System.assert(params.isEmpty());
119119
}
120120

121121
@isTest
122122
private static void ApiClient_paramsFromAnyPrimitiveTypeDiscardNull() {
123-
Swagger.ApiClient client = new Swagger.ApiClient();
124-
List<Swagger.Param> params = new List<Swagger.Param>();
123+
OAS.ApiClient client = new OAS.ApiClient();
124+
List<OAS.Param> params = new List<OAS.Param>();
125125
params.addAll(client.makeParam('foo', 'bar'));
126126
params.addAll(client.makeParam('foo', 10));
127127
params.addAll(client.makeParam('foo', 12.6));
@@ -141,13 +141,13 @@ private class SwaggerTest {
141141

142142
@isTest
143143
private static void ApiClient_requiredParameterPasses() {
144-
Swagger.ApiClient client = new Swagger.ApiClient();
144+
OAS.ApiClient client = new OAS.ApiClient();
145145
client.assertNotNull('foo', 'bar');
146146
}
147147

148148
@isTest
149149
private static void ApiClient_requiredParameterFails() {
150-
Swagger.ApiClient client = new Swagger.ApiClient();
150+
OAS.ApiClient client = new OAS.ApiClient();
151151
try {
152152
client.assertNotNull(null, 'bar');
153153
} catch (NullPointerException e) {
@@ -217,20 +217,20 @@ private class SwaggerTest {
217217
'"bat":false'
218218
};
219219
Set<String> actual1 = new Set<String>(client
220-
.toBody('application/json', body1, new List<Swagger.Param>())
220+
.toBody('application/json', body1, new List<OAS.Param>())
221221
.removeStart('{')
222222
.removeEnd('}')
223223
.split(',')
224224
);
225225
System.assertEquals(expected1, actual1);
226226

227227
String body2 = 'Hello, World!';
228-
String actual2 = client.toBody('text/plain', body2, new List<Swagger.Param>());
228+
String actual2 = client.toBody('text/plain', body2, new List<OAS.Param>());
229229
System.assertEquals(body2, actual2);
230230

231-
List<Swagger.Param> form = new List<Swagger.Param>{
232-
new Swagger.Param('hello', 'world'),
233-
new Swagger.Param('date', '2017-01-01 15:00:00')
231+
List<OAS.Param> form = new List<OAS.Param>{
232+
new OAS.Param('hello', 'world'),
233+
new OAS.Param('date', '2017-01-01 15:00:00')
234234
};
235235
String expected3 = 'hello=world&date=2017-01-01+15%3A00%3A00';
236236
String actual3 = client.toBody('application/x-www-form-urlencoded', '', form);
@@ -288,9 +288,9 @@ private class SwaggerTest {
288288
MockApiClient client = new MockApiClient();
289289
String path = '/departments/{department}';
290290
Map<String, Object> params = new Map<String, Object>{'department' => 'finance'};
291-
List<Swagger.Param> queryParams = new List<Swagger.Param>{
292-
new Swagger.Param('foo', 'bar'),
293-
new Swagger.Param('bat', '123')
291+
List<OAS.Param> queryParams = new List<OAS.Param>{
292+
new OAS.Param('foo', 'bar'),
293+
new OAS.Param('bat', '123')
294294
};
295295
String expected = 'callout:Winkelmeyer/departments/finance?foo=bar&bat=123';
296296
String actual = client.toEndpoint(path, params, queryParams);
@@ -301,7 +301,7 @@ private class SwaggerTest {
301301
private static void ApiClient_returnParsedBody() {
302302
MockApiClient client = new MockApiClient();
303303
HttpResponse res = new HttpResponse();
304-
SwaggerResponseMock mock = new SwaggerResponseMock(res);
304+
OASResponseMock mock = new OASResponseMock(res);
305305
Test.setMock(HttpCalloutMock.class, mock);
306306

307307
res.setStatus('OK');
@@ -314,8 +314,8 @@ private class SwaggerTest {
314314

315315
Address a = (Address) client.invoke(
316316
'GET', '/address', '',
317-
new List<Swagger.Param>(),
318-
new List<Swagger.Param>(),
317+
new List<OAS.Param>(),
318+
new List<OAS.Param>(),
319319
new Map<String, Object>(),
320320
new Map<String, Object>(),
321321
new List<String>{'application/json'},
@@ -337,16 +337,16 @@ private class SwaggerTest {
337337
private static void ApiClient_noReturnTypeReturnsNull() {
338338
MockApiClient client = new MockApiClient();
339339
HttpResponse res = new HttpResponse();
340-
SwaggerResponseMock mock = new SwaggerResponseMock(res);
340+
OASResponseMock mock = new OASResponseMock(res);
341341
Test.setMock(HttpCalloutMock.class, mock);
342342

343343
res.setStatus('OK');
344344
res.setStatusCode(200);
345345

346346
Object o = client.invoke(
347347
'POST', '/address', '',
348-
new List<Swagger.Param>(),
349-
new List<Swagger.Param>(),
348+
new List<OAS.Param>(),
349+
new List<OAS.Param>(),
350350
new Map<String, Object>(),
351351
new Map<String, Object>(),
352352
new List<String>{'application/json'},
@@ -358,7 +358,7 @@ private class SwaggerTest {
358358
System.assertEquals(null, o);
359359
}
360360

361-
private class MockApiClient extends Swagger.ApiClient {
361+
private class MockApiClient extends OAS.ApiClient {
362362
public MockApiClient() {
363363
basePath = 'https://blog.winkelmeyer.com';
364364
calloutName = 'Winkelmeyer';

modules/openapi-generator/src/main/resources/apex/README_ant.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ HttpBasicAuth {{{name}}} = (HttpBasicAuth) client.getAuthentication('{{{name}}}'
116116
ApiKeyAuth {{{name}}} = (ApiKeyAuth) client.getAuthentication('{{{name}}}');
117117
{{{name}}}.setApiKey('YOUR API KEY');{{/isApiKey}}{{#isOAuth}}
118118
// Configure OAuth2 access token for authorization: {{{name}}}
119-
Swagger.OAuth {{{name}}} = (Swagger.OAuth) client.getAuthentication('{{{name}}}');
119+
OAS.OAuth {{{name}}} = (OAS.OAuth) client.getAuthentication('{{{name}}}');
120120
{{{name}}}.setAccessToken('YOUR ACCESS TOKEN');{{/isOAuth}}
121121
{{/authMethods}}
122122
{{/hasAuthMethods}}
@@ -135,7 +135,7 @@ try {
135135
{{#returnType}}
136136
System.debug(result);
137137
{{/returnType}}
138-
} catch (Swagger.ApiException e) {
138+
} catch (OAS.ApiException e) {
139139
// ...handle your exceptions
140140
}{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
141141
```

modules/openapi-generator/src/main/resources/apex/README_sfdx.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ try {
6767
{{#returnType}}
6868
System.debug(result);
6969
{{/returnType}}
70-
} catch (Swagger.ApiException e) {
70+
} catch (OAS.ApiException e) {
7171
// ...handle your exceptions
7272
}{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
7373
```

modules/openapi-generator/src/main/resources/apex/api.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public class {{classname}} {
2525
{{#returnType}}
2626
* @return {{{returnType}}}
2727
{{/returnType}}
28-
* @throws Swagger.ApiException if fails to make API call
28+
* @throws OAS.ApiException if fails to make API call
2929
*/
3030
public {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}}({{#hasParams}}Map<String, Object> params{{/hasParams}}) {
3131
{{#allParams}}
3232
{{#required}}
3333
client.assertNotNull(params.get('{{paramName}}'), '{{paramName}}');
3434
{{/required}}
3535
{{/allParams}}
36-
List<Swagger.Param> query = new List<Swagger.Param>();
36+
List<OAS.Param> query = new List<OAS.Param>();
3737
{{#hasQueryParams}}
3838

3939
// cast query params to verify their expected type
@@ -54,7 +54,7 @@ public class {{classname}} {
5454

5555
{{/hasMore}}
5656
{{/queryParams}}
57-
List<Swagger.Param> form = new List<Swagger.Param>();
57+
List<OAS.Param> form = new List<OAS.Param>();
5858
{{#hasFormParams}}
5959

6060
// cast form params to verify their expected type

0 commit comments

Comments
 (0)