Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grails.databinding.converters

import grails.databinding.converters.ValueConverter;
import groovy.transform.CompileStatic

@CompileStatic
class UUIDConverter implements ValueConverter {

@Override
boolean canConvert(value) {
value instanceof String
}

@Override
def convert(value) {
if(value) {
try {
return UUID.fromString(value as String)
} catch(IllegalArgumentException ignore) {
return null
}
}
return null
}

@Override
Class<?> getTargetType() {
UUID
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.grails.databinding.converters


import grails.databinding.SimpleMapDataBindingSource

import grails.databinding.SimpleDataBinder

import spock.lang.Specification

class UUIDConversionSpec extends Specification {

void 'Binding String to a UUID'() {
given:
def binder = new SimpleDataBinder()
binder.registerConverter new UUIDConverter()
def testClass = new UUIDTestClass()

and:
def givenUUID = '534f7cee-bf88-45f3-96f2-9cae0828cd16'

when:
binder.bind testClass, [uuid: givenUUID] as SimpleMapDataBindingSource

then:
testClass.uuid instanceof UUID
testClass.uuid.toString() == givenUUID
}

void 'Binding badly formatted string to a UUID'() {
given:
def binder = new SimpleDataBinder()
binder.registerConverter new UUIDConverter()
def testClass = new UUIDTestClass()

and:
def givenUUID = '123-not-a-uuid-3291'

when:
binder.bind testClass, [uuid: givenUUID] as SimpleMapDataBindingSource

then:
notThrown(IllegalArgumentException)
testClass.uuid == null
}

void 'Binding null to UUID'() {
given:
def binder = new SimpleDataBinder()
binder.registerConverter new UUIDConverter()
def testClass = new UUIDTestClass()

and:
def givenUUID = null

when:
binder.bind testClass, [uuid: givenUUID] as SimpleMapDataBindingSource

then:
notThrown(IllegalArgumentException)
testClass.uuid == null
}
}

class UUIDTestClass {
UUID uuid
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import grails.plugins.Plugin
import grails.util.GrailsUtil
import grails.web.databinding.DataBindingUtils
import grails.web.databinding.GrailsWebDataBinder
import org.grails.databinding.converters.UUIDConverter
import org.grails.web.databinding.bindingsource.DataBindingSourceRegistry
import org.grails.web.databinding.bindingsource.DefaultDataBindingSourceRegistry
import org.grails.web.databinding.bindingsource.HalJsonDataBindingSourceCreator
Expand Down Expand Up @@ -69,6 +70,7 @@ class DataBindingGrailsPlugin extends Plugin {
}

timeZoneConverter(TimeZoneConverter)
uuidConverter(UUIDConverter)

defaultDateConverter(DateConversionHelper) {
formatStrings = dateFormats
Expand Down