Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java Jersey] Update ApiClient.mustache Jersey doesn't allow entities in method DELETE #19530

Merged
merged 25 commits into from
Sep 16, 2024

Conversation

eric-rolli
Copy link
Contributor

@eric-rolli eric-rolli commented Sep 5, 2024

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.6.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Jersey doesn't allow request entities in method DELETE
Jersey doesn't allow entities in method DELETE
Jersey doesn't allow entities in method DELETE
Jersey doesn't allow entities in method DELETE
Jersey doesn't allow entities in method DELETE
Jersey doesn't allow entities in method DELETE
Jersey doesn't allow entities in method DELETE
Jersey doesn't allow entities in method DELETE
Jersey doesn't allow entities in method DELETE
Jersey doesn't allow entities in method DELETE
Jersey doesn't allow entities in method DELETE
@eric-rolli
Copy link
Contributor Author

@bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg
Correction for Java Jersey2/3 generation.

@eric-rolli eric-rolli changed the title Update Jersey ApiClient.mustache Jersey doesn't allow entities in method DELETE [Java Jersey] Update ApiClient.mustache Jersey doesn't allow entities in method DELETE Sep 5, 2024
@wing328
Copy link
Member

wing328 commented Sep 7, 2024

@eric-rolli thanks for the PR

If I remember correctly, some users need send the payload in DELETE request: #13457

Given that your use cases do not require sending payload in DELETE request, what about leaving the code as it's?

@eric-rolli
Copy link
Contributor Author

eric-rolli commented Sep 9, 2024

@wing328
The problem is that the jersey library enforces that the entity must be null.
See org.glassfish.jersey.client.JerseyInvocation
https://github.com/eclipse-ee4j/jersey/blob/2.x/core-client/src/main/java/org/glassfish/jersey/client/JerseyInvocation.java
https://github.com/eclipse-ee4j/jersey/blob/4.0/core-client/src/main/java/org/glassfish/jersey/client/JerseyInvocation.java

private static Map<String, EntityPresence> initializeMap() {
	HashMap<String, EntityPresence> map = new HashMap<String, EntityPresence>();
	map.put("DELETE", EntityPresence.MUST_BE_NULL);
	map.put("GET", EntityPresence.MUST_BE_NULL);
	map.put("HEAD", EntityPresence.MUST_BE_NULL);
	map.put("OPTIONS", EntityPresence.OPTIONAL);
	map.put("PATCH", EntityPresence.MUST_BE_PRESENT);
	map.put("POST", EntityPresence.OPTIONAL);
	map.put("PUT", EntityPresence.MUST_BE_PRESENT);
	map.put("TRACE", EntityPresence.MUST_BE_NULL);
	return map;
}

private void validateHttpMethodAndEntity(ClientRequest request) {
	String method;
	EntityPresence entityPresence;
	boolean suppressExceptions = PropertiesHelper.isProperty((Object) request.getConfiguration()
			.getProperty("jersey.config.client.suppressHttpComplianceValidation"));
	Object shcvProperty = request.getProperty("jersey.config.client.suppressHttpComplianceValidation");
	if (shcvProperty != null) {
		suppressExceptions = PropertiesHelper.isProperty((Object) shcvProperty);
	}
	if ((entityPresence = METHODS
			.get((method = request.getMethod()).toUpperCase(Locale.ROOT))) == EntityPresence.MUST_BE_NULL
			&& request.hasEntity()) {
		if (!suppressExceptions)
			throw new IllegalStateException(
					LocalizationMessages.ERROR_HTTP_METHOD_ENTITY_NOT_NULL((Object) method));
		LOGGER.warning(LocalizationMessages.ERROR_HTTP_METHOD_ENTITY_NOT_NULL((Object) method));
		return;
	} else {
		if (entityPresence != EntityPresence.MUST_BE_PRESENT || request.hasEntity())
			return;
		if (!suppressExceptions)
			throw new IllegalStateException(LocalizationMessages.ERROR_HTTP_METHOD_ENTITY_NULL((Object) method));
		LOGGER.warning(LocalizationMessages.ERROR_HTTP_METHOD_ENTITY_NULL((Object) method));
	}
}

But the current openapi generated code always produces a non-null Entity object, even if the body in null.
See method serialize in ApiClient.mustache
For a null Body the Entity object will contain an empty string instead of null!

The result is that DELETE methods are not usable with the code generated for the jersey library.

Would the following modification be OK?
} else if ("DELETE".equals(method)) {
if ("".equals(entity.getEntity())) {
response = invocationBuilder.method("DELETE");
} else {
response = invocationBuilder.method("DELETE", entity);
}

@wing328
Copy link
Member

wing328 commented Sep 13, 2024

Would the following modification be OK?

I think we can give it a try

@eric-rolli
Copy link
Contributor Author

@wing328
I made the changes to send the entity only if it is not empty

@wing328 wing328 merged commit bbafeae into OpenAPITools:master Sep 16, 2024
64 checks passed
@wing328
Copy link
Member

wing328 commented Sep 16, 2024

thanks. let's give it a try

please test it later after snapshot version published (or test it locally)

@wing328 wing328 added this to the 7.9.0 milestone Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants