generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ConvertibleValuesDeserializer (#448)
Should fix BookR2dbcSchemaMultiTenancySpec#testRest from micronaut-projects/micronaut-data#2175
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...src/main/java/io/micronaut/serde/support/deserializers/ConvertibleValuesDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2017-2023 original 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 | ||
* | ||
* https://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 io.micronaut.serde.support.deserializers; | ||
|
||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.convert.ConversionService; | ||
import io.micronaut.core.convert.value.ConvertibleValues; | ||
import io.micronaut.core.convert.value.MutableConvertibleValuesMap; | ||
import io.micronaut.core.type.Argument; | ||
import io.micronaut.json.convert.JsonNodeConvertibleValues; | ||
import io.micronaut.json.tree.JsonNode; | ||
import io.micronaut.serde.Decoder; | ||
import io.micronaut.serde.Deserializer; | ||
import io.micronaut.serde.exceptions.SerdeException; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
@Singleton | ||
@SuppressWarnings("rawtypes") | ||
final class ConvertibleValuesDeserializer implements Deserializer<ConvertibleValues> { | ||
@NonNull | ||
private final ConversionService conversionService; | ||
|
||
ConvertibleValuesDeserializer(@NonNull ConversionService conversionService) { | ||
this.conversionService = conversionService; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Deserializer<ConvertibleValues> createSpecific(@NonNull DecoderContext context, @NonNull Argument<? super ConvertibleValues> type) throws SerdeException { | ||
Optional<Argument<?>> var = type.getFirstTypeVariable(); | ||
if (var.isPresent()) { | ||
//noinspection unchecked | ||
return new Specialized((Argument<Object>) var.get(), (Deserializer<Object>) context.findDeserializer(var.get())); | ||
} else { | ||
return this; | ||
} | ||
} | ||
|
||
@Override | ||
public ConvertibleValues deserialize(@NonNull Decoder decoder, @NonNull DecoderContext context, @NonNull Argument<? super ConvertibleValues> type) throws IOException { | ||
JsonNode node = decoder.decodeNode(); | ||
if (!node.isObject()) { | ||
throw decoder.createDeserializationException("Expected object", node); | ||
} | ||
return new JsonNodeConvertibleValues(node, conversionService); | ||
} | ||
|
||
private class Specialized implements Deserializer<ConvertibleValues> { | ||
@Nullable | ||
private final Argument<Object> componentType; | ||
@Nullable | ||
private final Deserializer<Object> componentDeserializer; | ||
|
||
Specialized(@Nullable Argument<Object> componentType, @Nullable Deserializer<Object> componentDeserializer) { | ||
this.componentType = componentType; | ||
this.componentDeserializer = componentDeserializer; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public ConvertibleValues deserialize(@NonNull Decoder decoder, @NonNull DecoderContext context, @NonNull Argument<? super ConvertibleValues> type) throws IOException { | ||
Decoder obj = decoder.decodeObject(type); | ||
MutableConvertibleValuesMap map = new MutableConvertibleValuesMap(); | ||
map.setConversionService(conversionService); | ||
while (true) { | ||
String key = obj.decodeKey(); | ||
if (key == null) { | ||
break; | ||
} | ||
map.put(key, componentDeserializer.deserialize(decoder, context, componentType)); | ||
} | ||
obj.finishStructure(); | ||
return map; | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
.../groovy/io/micronaut/serde/support/deserializers/ConvertibleValuesDeserializerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.micronaut.serde.support.deserializers | ||
|
||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.core.convert.value.ConvertibleValues | ||
import io.micronaut.core.type.Argument | ||
import io.micronaut.json.JsonMapper | ||
import io.micronaut.serde.exceptions.SerdeException | ||
import spock.lang.Specification | ||
|
||
class ConvertibleValuesDeserializerSpec extends Specification { | ||
// this mostly matches the spec from micronaut-jackson-databind | ||
|
||
def 'test without value type'() { | ||
given: | ||
def ctx = ApplicationContext.run() | ||
def mapper = ctx.getBean(JsonMapper) | ||
|
||
when: | ||
def values = mapper.readValue('{"foo":"bar","fizz":[4]}', Argument.of(ConvertibleValues)) | ||
then: | ||
values.names() == ["foo", "fizz"] as Set | ||
values.get("foo", String).get() == "bar" | ||
values.get("fizz", Argument.of(List, int)).get() == [4] | ||
|
||
cleanup: | ||
ctx.close() | ||
} | ||
|
||
def 'test with value type'() { | ||
given: | ||
def ctx = ApplicationContext.run() | ||
def mapper = ctx.getBean(JsonMapper) | ||
|
||
when: | ||
def values = mapper.readValue('{"foo":"4","fizz":5}', Argument.of(ConvertibleValues, Integer)) | ||
then: | ||
values.names() == ["foo", "fizz"] as Set | ||
// values should be int-typed | ||
values.get("foo", Object).get() == 4 | ||
values.get("fizz", Object).get() == 5 | ||
|
||
cleanup: | ||
ctx.close() | ||
} | ||
|
||
def 'test wrong format'() { | ||
given: | ||
def ctx = ApplicationContext.run() | ||
def mapper = ctx.getBean(JsonMapper) | ||
|
||
when: | ||
mapper.readValue('[{"foo":"4","fizz":5},4]', Argument.of(ConvertibleValues)) | ||
then: | ||
thrown SerdeException | ||
|
||
cleanup: | ||
ctx.close() | ||
} | ||
} |