spring-hateoas icon indicating copy to clipboard operation
spring-hateoas copied to clipboard

When @EnableHypermediaSupport has no types, only enable user-defined media types.

Open gregturn opened this issue 6 years ago • 6 comments
trafficstars

If an empty list of hypermedia types is provided, activate all of them.

Also consider making an empty list the default value for this annotation.

gregturn avatar Aug 31 '19 13:08 gregturn

When using an empty list, the old code had this:

if (types.isEmpty()) {
	return configurationProviders.toArray(new String[0]);
}

This was incorrect, and would produce an ArrayStoreException.

Instead, the purpose is to enable ALL the MediaTypeConfigurationProvider found in the application context, like this:

if (types.isEmpty()) {
	return configurationProviders.stream() //
			.map(MediaTypeConfigurationProvider::getConfiguration) //
			.map(Class::getName) //
			.toArray(String[]::new);
}

gregturn avatar Aug 31 '19 13:08 gregturn

Since a Stream must be created in either situation, this may be preferable:

return configurationProviders.stream() //
		.filter(it -> {
			// If there are no types, then let them all through
			if (types.isEmpty()) {
				return true;
			}
			// Filter the ones supporting the given media types
			return it.supportsAny(types);
		}) //
		.map(MediaTypeConfigurationProvider::getConfiguration) //
		.map(Class::getName) //
		.toArray(String[]::new);

gregturn avatar Aug 31 '19 13:08 gregturn

Duplicates #1015 .

gregturn avatar Sep 05 '19 15:09 gregturn

UPDATE: If enabled but empty, then don't register the pre-built ones. BUT activate custom ones.

Issue a warning if NO hypermedia types are found.

gregturn avatar May 05 '20 17:05 gregturn

As discussed in a video call, we are switching it up so that enabling hypermedia support with no specified types, Spring HATEOAS will attempt to ONLY enable user-defined types.

If NO media types are found, issue a warning that you've enabled hypermedia support but not provided a media type.

This will avoid enabling newly added types from the team.

May want to include a custom annotation where the user can define a mediatype, but shield it from being picked up.

gregturn avatar May 06 '20 14:05 gregturn

Because @EnableHypermediaSupport kicks in early in the lifecycle, we can't look for HypermediaMappingInformation types at that stage.

Only alternative to ANY sort of check sounds like some lifecycle event handler that validates right before the app goes active.

gregturn avatar Aug 04 '20 19:08 gregturn