Skip to content

Commit edb88d9

Browse files
authored
Delete body angular (OpenAPITools#10751)
* change isBodyAllowed to return true for delete method Add method isMethodPutOrPatchOrPost * change templates to use isMethodPutOrPatchOrPost to be compatible with previous versions, lubrary mainterners should decide if want to use isBodyAllowed * since angular 12.1 delete with body is supported Change angular typescript to support delete with body if angular version is >= 12.1 update default ngVersion to be latest 12.2.12 * generate samples and docs * ApiClient was OK revert my changes
1 parent 359e393 commit edb88d9

File tree

7 files changed

+34
-13
lines changed

7 files changed

+34
-13
lines changed

docs/generators/typescript-angular.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
1919
|modelFileSuffix|The suffix of the file of the generated model (model<suffix>.ts).| |null|
2020
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name. Only change it if you provide your own run-time code for (de-)serialization of models| |original|
2121
|modelSuffix|The suffix of the generated model.| |null|
22-
|ngVersion|The version of Angular. (At least 6.0.0)| |12.0.0|
22+
|ngVersion|The version of Angular. (At least 6.0.0)| |12.2.12|
2323
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
2424
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
2525
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,17 @@ public boolean isRestfulUpdate() {
225225
/**
226226
* Check if body param is allowed for the request method
227227
*
228-
* @return true request method is PUT, PATCH or POST; false otherwise
228+
* @return true request method is DELETE, PUT, PATCH or POST; false otherwise
229229
*/
230230
public boolean isBodyAllowed() {
231+
return Arrays.asList("DELETE","PUT", "PATCH", "POST").contains(httpMethod.toUpperCase(Locale.ROOT));
232+
}
233+
/**
234+
* Check if the request method is PUT or PATCH or POST
235+
*
236+
* @return true request method is PUT, PATCH or POST; false otherwise
237+
*/
238+
public boolean isMethodPutOrPatchOrPost() {
231239
return Arrays.asList("PUT", "PATCH", "POST").contains(httpMethod.toUpperCase(Locale.ROOT));
232240
}
233241

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static enum PROVIDED_IN_LEVEL {none, root, any, platform}
5555
public static final String PROVIDED_IN = "providedIn";
5656
public static final String ENFORCE_GENERIC_MODULE_WITH_PROVIDERS = "enforceGenericModuleWithProviders";
5757
public static final String HTTP_CONTEXT_IN_OPTIONS = "httpContextInOptions";
58+
public static final String DELETE_ACCEPTS_BODY = "deleteAcceptsBody";
5859
public static final String API_MODULE_PREFIX = "apiModulePrefix";
5960
public static final String CONFIGURATION_PREFIX = "configurationPrefix";
6061
public static final String SERVICE_SUFFIX = "serviceSuffix";
@@ -66,7 +67,7 @@ public static enum PROVIDED_IN_LEVEL {none, root, any, platform}
6667
public static final String STRING_ENUMS_DESC = "Generate string enums instead of objects for enum values.";
6768
public static final String QUERY_PARAM_OBJECT_FORMAT = "queryParamObjectFormat";
6869

69-
protected String ngVersion = "12.0.0";
70+
protected String ngVersion = "12.2.12";
7071
protected String npmRepository = null;
7172
private boolean useSingleRequestParameter = false;
7273
protected String serviceSuffix = "Service";
@@ -237,6 +238,12 @@ public void processOpts() {
237238
additionalProperties.put(HTTP_CONTEXT_IN_OPTIONS, false);
238239
}
239240

241+
if (ngVersion.atLeast("12.1.0")) {
242+
additionalProperties.put(DELETE_ACCEPTS_BODY, true);
243+
} else {
244+
additionalProperties.put(DELETE_ACCEPTS_BODY, false);
245+
}
246+
240247
additionalProperties.put(NG_VERSION, ngVersion);
241248

242249
if (additionalProperties.containsKey(API_MODULE_PREFIX)) {

modules/openapi-generator/src/main/resources/Java/libraries/google-api-client/api.mustache

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class {{classname}} {
121121
String localVarUrl = uriBuilder{{#hasPathParams}}.buildFromMap(uriVariables).toString();{{/hasPathParams}}{{^hasPathParams}}.build().toString();{{/hasPathParams}}
122122
GenericUrl genericUrl = new GenericUrl(localVarUrl);
123123

124-
HttpContent content = {{#isBodyAllowed}}{{#bodyParam}}apiClient.new JacksonJsonHttpContent({{paramName}}){{/bodyParam}}{{^bodyParam}}new EmptyContent(){{/bodyParam}}{{/isBodyAllowed}}{{^isBodyAllowed}}null{{/isBodyAllowed}};
124+
HttpContent content = {{#isMethodPutOrPatchOrPost}}{{#bodyParam}}apiClient.new JacksonJsonHttpContent({{paramName}}){{/bodyParam}}{{^bodyParam}}new EmptyContent(){{/bodyParam}}{{/isMethodPutOrPatchOrPost}}{{^isMethodPutOrPatchOrPost}}null{{/isMethodPutOrPatchOrPost}};
125125
return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
126126
}{{#bodyParam}}
127127

@@ -199,7 +199,7 @@ public class {{classname}} {
199199
String localVarUrl = uriBuilder{{#hasPathParams}}.buildFromMap(uriVariables).toString();{{/hasPathParams}}{{^hasPathParams}}.build().toString();{{/hasPathParams}}
200200
GenericUrl genericUrl = new GenericUrl(localVarUrl);
201201

202-
HttpContent content = {{#isBodyAllowed}}{{#bodyParam}}apiClient.new JacksonJsonHttpContent({{paramName}}){{/bodyParam}}{{^bodyParam}}new EmptyContent(){{/bodyParam}}{{/isBodyAllowed}}{{^isBodyAllowed}}null{{/isBodyAllowed}};
202+
HttpContent content = {{#isMethodPutOrPatchOrPost}}{{#bodyParam}}apiClient.new JacksonJsonHttpContent({{paramName}}){{/bodyParam}}{{^bodyParam}}new EmptyContent(){{/bodyParam}}{{/isMethodPutOrPatchOrPost}}{{^isMethodPutOrPatchOrPost}}null{{/isMethodPutOrPatchOrPost}};
203203
return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
204204
}
205205

modules/openapi-generator/src/main/resources/erlang-proper/api.mustache

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
Method = {{httpMethod}},
1919
Host = application:get_env({{packageName}}, host, "http://localhost:8080"),
2020
Path = ["{{{replacedPathName}}}"],
21-
{{#isBodyAllowed}}
21+
{{#isMethodPutOrPatchOrPost}}
2222
Body = {{^formParams.isEmpty}}{form, [{{#formParams}}{{#required}}{{^-first}}, {{/-first}}{<<"{{{baseName}}}">>, {{paramName}}{{/required}}{{/formParams}}]++{{packageName}}_utils:optional_params([{{#formParams}}{{^required}}{{^-first}}, {{/-first}}'{{{baseName}}}'{{/required}}{{/formParams}}], _OptionalParams)}{{/formParams.isEmpty}}{{#formParams.isEmpty}}{{#bodyParams.isEmpty}}[]{{/bodyParams.isEmpty}}{{^bodyParams.isEmpty}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/bodyParams.isEmpty}}{{/formParams.isEmpty}},
2323
ContentType = {{#hasConsumes}}hd([{{#consumes}}{{^-first}}, {{/-first}}"{{mediaType}}"{{/consumes}}]){{/hasConsumes}}{{^hasConsumes}}"text/plain"{{/hasConsumes}},
24-
{{/isBodyAllowed}}
24+
{{/isMethodPutOrPatchOrPost}}
2525
{{^queryParams.isEmpty}}
2626
QueryString = [{{#queryParams}}{{^-first}}, {{/-first}}<<"{{{baseName}}}=">>, {{{paramName}}}, <<"&">>{{/queryParams}}],
2727
{{/queryParams.isEmpty}}
2828

29-
{{#isBodyAllowed}}
29+
{{#isMethodPutOrPatchOrPost}}
3030
{{packageName}}_utils:request(Method, [Host, ?BASE_URL, Path{{^queryParams.isEmpty}}, <<"?">>, QueryString{{/queryParams.isEmpty}}], jsx:encode(Body), ContentType).
31-
{{/isBodyAllowed}}
32-
{{^isBodyAllowed}}
31+
{{/isMethodPutOrPatchOrPost}}
32+
{{^isMethodPutOrPatchOrPost}}
3333
{{packageName}}_utils:request(Method, [Host, ?BASE_URL, Path{{^queryParams.isEmpty}}, <<"?">>, QueryString{{/queryParams.isEmpty}}]).
34-
{{/isBodyAllowed}}
34+
{{/isMethodPutOrPatchOrPost}}
3535

3636
{{/operation}}
3737
{{/operations}}

modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache

+6
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,14 @@ export class {{classname}} {
355355
}
356356

357357
{{/isResponseFile}}
358+
{{#deleteAcceptsBody}}
358359
return this.httpClient.{{httpMethod}}{{^isResponseFile}}<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>{{/isResponseFile}}(`${this.configuration.basePath}{{{path}}}`,{{#isBodyAllowed}}
359360
{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}{{#hasFormParams}}localVarConvertFormParamsToString ? localVarFormParams.toString() : localVarFormParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}}{{/bodyParam}},{{/isBodyAllowed}}
361+
{{/deleteAcceptsBody}}
362+
{{^deleteAcceptsBody}}
363+
return this.httpClient.{{httpMethod}}{{^isResponseFile}}<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>{{/isResponseFile}}(`${this.configuration.basePath}{{{path}}}`,{{#isMethodPutOrPatchOrPost}}
364+
{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}{{#hasFormParams}}localVarConvertFormParamsToString ? localVarFormParams.toString() : localVarFormParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}}{{/bodyParam}},{{/isMethodPutOrPatchOrPost}}
365+
{{/deleteAcceptsBody}}
360366
{
361367
{{#httpContextInOptions}}
362368
context: localVarHttpContext,

modules/openapi-generator/src/main/resources/typescript-nestjs/api.service.mustache

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ export class {{classname}} {
208208
{{/formParams}}
209209

210210
{{/hasFormParams}}
211-
return this.httpClient.{{httpMethod}}{{^isResponseFile}}<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>{{/isResponseFile}}(`${this.basePath}{{{path}}}`,{{#isBodyAllowed}}
212-
{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}{{#hasFormParams}}convertFormParamsToString ? formParams.toString() : formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}}{{/bodyParam}},{{/isBodyAllowed}}
211+
return this.httpClient.{{httpMethod}}{{^isResponseFile}}<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>{{/isResponseFile}}(`${this.basePath}{{{path}}}`,{{#isMethodPutOrPatchOrPost}}
212+
{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}{{#hasFormParams}}convertFormParamsToString ? formParams.toString() : formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}}{{/bodyParam}},{{/isMethodPutOrPatchOrPost}}
213213
{
214214
{{#hasQueryParams}}
215215
params: queryParameters,

0 commit comments

Comments
 (0)