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

Consider mapping empty form values to [null] for Object properties [SPR-7544] #12201

Closed
spring-projects-issues opened this issue Sep 11, 2010 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Sep 11, 2010

Keith Donald opened SPR-7544 and commented

Currently empty form fields are mapped to "" (empty string). This has several issues:

  1. Neither a @NotNull JSR-303 constraint or a "not null" relational DB constraint fails in this case. You're left with defining a min @Size constraint or relying on a implementation-specific @NotEmpty. In addition, there is no DB min constraint you can define at the Db level. Simply mapping "" to null would solve these problems and we could just rely on @NotNull/not null.

Affects: 3.0.4

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Keith Donald commented

I was able to plug in a empty String -> [null] mapping behavior by using a custom Converter<String, String>:

public class ConversionServiceFactoryBean implements FactoryBean<ConversionService> {

	public Class<?> getObjectType() {
		return ConversionService.class;
	}

	public boolean isSingleton() {
		return true;
	}

	public ConversionService getObject() throws Exception {
		FormattingConversionService conversionService = new FormattingConversionService();
		ConversionServiceFactory.addDefaultConverters(conversionService);
		conversionService.addConverter(new EmptyStringToNullConverter());
		new JodaTimeFormattingConfigurer().installJodaTimeFormatting(conversionService);			
		return conversionService;
 	}

	private static class EmptyStringToNullConverter implements Converter<String, String> {
		public String convert(String source) {
			return source.isEmpty() ? null : source;
		}
	}
}

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 5, 2011

Rossen Stoyanchev commented

This relates to #10090 to the extent that #10090 expects the target field to be set to "" (rather than null) given an empty string request parameter.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jun 3, 2011

Keith Donald commented

There is also an inconsistency between how String fields are bound depending on if a formatter is registered or not, as pointed out by #12969 and #12038.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 10, 2011

Rossen Stoyanchev commented

A duplicate of #12038.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants