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

Use bean context classloader for Jackson #10121

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -32,6 +32,7 @@
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import io.micronaut.context.BeanContext;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Primary;
Expand Down Expand Up @@ -70,6 +71,9 @@ public class ObjectMapperFactory {
@Inject
protected ConversionService conversionService;

@Inject
protected BeanContext beanContext;

@Inject
// have to be fully qualified due to JDK Module type
protected com.fasterxml.jackson.databind.Module[] jacksonModules = new com.fasterxml.jackson.databind.Module[0];
Expand Down Expand Up @@ -143,6 +147,10 @@ public ObjectMapper objectMapper(@Nullable JacksonConfiguration jacksonConfigura
objectMapper.findAndRegisterModules();
}
objectMapper.registerModules(jacksonModules);
if (beanContext != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some cases where ObjectMapperFactory is used without the bean context

objectMapper.setTypeFactory(objectMapper.getTypeFactory().withClassLoader(beanContext.getClassLoader()));
}

SimpleModule module = new SimpleModule(MICRONAUT_MODULE);
module.setDeserializers(new MicronautDeserializers(conversionService));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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
*
* 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 io.micronaut.jackson

import io.micronaut.annotation.processing.test.AbstractTypeElementSpec
import io.micronaut.context.ApplicationContext
import io.micronaut.core.type.Argument
import io.micronaut.json.JsonMapper

class JacksonClassloaderSetupSpec extends AbstractTypeElementSpec {

void "test Jackson classloader"() {
given:
def context = buildContext('test.Animal',"""
package test;

import com.fasterxml.jackson.annotation.*;

@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, defaultImpl = Dog.class)
interface Animal {
String getName();
}

class Dog implements Animal {
private String name;
private double barkVolume;

@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setBarkVolume(double barkVolume) {
this.barkVolume = barkVolume;
}
public double getBarkVolume() {
return barkVolume;
}
}

class Cat implements Animal {
private String name;
private int lives;
private boolean likesCream;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLives() {
return lives;
}
public void setLives(int lives) {
this.lives = lives;
}
public void setLikesCream(boolean likesCream) {
this.likesCream = likesCream;
}
public boolean isLikesCream() {
return likesCream;
}
}
""", true)

when:
JsonMapper jsonMapper = context.getBean(JsonMapper)

def dogBean = jsonMapper.readValue('{"@c":".Dog","name":"Fred","barkVolume":1.1}', argumentOf(context, 'test.Animal'))
def catBean = jsonMapper.readValue('{"@c":".Cat","name":"Joe","lives":9,"likesCream":true}', argumentOf(context, 'test.Animal'))

then:
dogBean.name == "Fred"
dogBean.barkVolume == 1.1d
catBean.name == "Joe"
catBean.likesCream
catBean.lives == 9

cleanup:
context.close()
}

Argument<Object> argumentOf(ApplicationContext context, String name) {
return Argument.of(context.classLoader.loadClass(name))
}
}
Loading