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

io.swagger.v3.core.util.Json public static method mapper() is not thread safe #10

Closed
walkingbear opened this issue May 17, 2024 · 1 comment

Comments

@walkingbear
Copy link

The public static method mapper() like below is not thread safe. It appears that the "mapper" field is designed to be lazily initialized and only initialize once. But current implementation does not guarantee the mapper field is initialized only once. If we want the lazy and only once initialization semantic, we need to use double checked locking or simply synchronize the mapper() method, otherwise, the mapper has chance to be initialized multiple times.

current implementation:

    private static ObjectMapper mapper;

    public static ObjectMapper mapper() {
        if (mapper == null) {
            mapper = ObjectMapperFactory.createJson();
        }
        return mapper;
    }

suggested implementation using double checked locking

public static ObjectMapper mapper() {
        if (mapper == null) {
            synchronized(Json.class) {
                if (mapper == null) {
                    mapper = ObjectMapperFactory.createJson();
                }
        }
}
        
@walkingbear
Copy link
Author

Closing this and opened swagger-api/swagger-core#4672 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant