sahaya / rest-assured

Automatically exported from code.google.com/p/rest-assured
0 stars 0 forks source link

Configuring RestAssured #209

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Setting up a custom object mapper for serialization

I think this is more of a "user doesnt understand how to use it right" kind of 
problem. I dont get how to set different configuration aspects (decoding, 
encoding, objectmapp and so forth) for a single configuration. Heres what i 
tried to do:

// setting default en- & decoding:
RestAssured.config = new RestAssuredConfig();
RestAssured.config = 
RestAssured.config.encoderConfig(EncoderConfig.encoderConfig().defaultContentCha
rset("UTF-8"));
RestAssured.config = 
RestAssured.config.decoderConfig(DecoderConfig.decoderConfig().defaultContentCha
rset("UTF-8"));

//afterwards I try to setup a custom object mapper:
RestAssured.config = 
RestAssured.config.objectMapperConfig(getGsonObjectMapperConfig());

//here is the definition of the above used method getGsonObjectMapperConfig:
private static ObjectMapperConfig getGsonObjectMapperConfig() {
        return ObjectMapperConfig.objectMapperConfig().gsonObjectMapperFactory(new GsonObjectMapperFactory() {
            public Gson create(Class cls, String charset) {
                return new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").setExclusionStrategies(exclusionStrategy).create();
            }
        });
    }

The de- and encoding seem to work fine but whenever a date is serialized it 
gets serialized as a Timestamp instead of the configured date format string. 
Could you please give em a hint. ^^

Best regards
Ben

P.S. RestAssured is awesome.

Original issue reported on code.google.com by roth.ben...@gmail.com on 28 Nov 2012 at 10:34

GoogleCodeExporter commented 9 years ago
oh damn. I forgot to remove the setExclusionStrategies - Part. The actual 
version of the method is:

private static ObjectMapperConfig getGsonObjectMapperConfig() {
        return ObjectMapperConfig.objectMapperConfig().gsonObjectMapperFactory(new GsonObjectMapperFactory() {
            public Gson create(Class cls, String charset) {
                return new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
            }
        });
    }

Original comment by roth.ben...@gmail.com on 28 Nov 2012 at 10:46

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Are you sure that GSON is actually used? If you have multiple Json mappers in 
classpath GSON may not be picked by classpath scanning. You can configure 
RestAssured to always use GSON by specifying the "defaultObjectMapperType" to 
GSON in the ObjectMapperConfig. Please tell me if this works.

Original comment by johan.ha...@gmail.com on 29 Nov 2012 at 7:13

GoogleCodeExporter commented 9 years ago
I couldn't find the aforementioned setter methods so I tried using the 
following instead:

RestAssuredConfig  restAssuredConfig = new RestAssuredConfig();
restAssuredConfig.decoderConfig(new DecoderConfig("UTF-8"));
restAssuredConfig.encoderConfig(new EncoderConfig("UTF-8", "UTF-8"));
restAssuredConfig.objectMapperConfig(ObjectMapperConfig.objectMapperConfig().def
aultObjectMapperType(ObjectMapperType.GSON)); // (1)
restAssuredConfig.objectMapperConfig(getGsonObjectMapperConfig()); // (2)
RestAssured.config = restAssuredConfig;

I couldnt find any other way to set the default object mapper but my guess 
would be that (1) would get overwritten by (2).
The code above does not work at all. Not even the encoding settings are picked 
up. Is it possible that the setter methods (setEncoderConfig, etc... are not 
part of RestAssured Version 1.7.1)?

Original comment by roth.ben...@gmail.com on 30 Nov 2012 at 9:51

GoogleCodeExporter commented 9 years ago
Actually the implementation didn't work as I though it would, I'm sorry to 
confuse you. The RestAssuredConfig is immutable so just as you say the examples 
that you show overrides one another. Try something like this:

RestAssured.config = new RestAssuredConfig().
            decoderConfig(
                    new DecoderConfig("UTF-8")
            ).encoderConfig(
                    new EncoderConfig("UTF-8", "UTF-8")
            ).objectMapperConfig(
                new ObjectMapperConfig(GSON).
                gsonObjectMapperFactory(myCustomObjectMapperFactory())
            );

I can see that this is very confusing. Try it and see if it works.

Original comment by johan.ha...@gmail.com on 30 Nov 2012 at 10:53

GoogleCodeExporter commented 9 years ago
Works like a charm. Thanks a lot.

Original comment by roth.ben...@gmail.com on 4 Dec 2012 at 1:37

GoogleCodeExporter commented 9 years ago
Great! I'll see if it can make it simpler in the future.

Original comment by johan.ha...@gmail.com on 4 Dec 2012 at 1:51

GoogleCodeExporter commented 9 years ago
I won't take any further action on this now.

Original comment by johan.ha...@gmail.com on 5 Dec 2012 at 9:10