-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Allow to define a default DocTypeTransformer for each content type #2770
Comments
Having a
For a transformer which can convert any JSON into a Java type, the |
@jkronegg For example, you can have a transformer method for the
And you can also have a transformer method for the
Lastly, you can have a fallback transformer, which is used if no transformers have been found for the corresponding
|
@driver733 yes, sorry, I was unclear. I was suggesting to add a doctype transformer interface such as: interface MultiDocTypeTransformer {
/**
Returns true if a content of the given contentType can be converted to toType.
*/
public boolean canConvert(String contentType, Type toType);
/**
* Converts a string content of the given contentType to the toType type. Throws an exception when not possible.
*/
public Object convert(String content, String contentType, Type toType);
} Then your implementation would be: class MyMultiDocTypeTransformer implements MultiDocTypeTransformer {
public boolean canConvert(String contentType, Type toType) {
return "json".equals(contentType);
}
public Object convert(String json, String contentType, Type toType) {
return objectMapper.readValue(json, objectMapper.constructType(toType));
}
} Wouldn't be this approach more flexible than the default doctype transformer you proposes ? (this is a simple question, not a proposal, because I didn't check the feasibility). |
@jkronegg |
🤔 What's the problem you're trying to solve?
I am trying to find a way to define a single common
DocType
transformer for acontentType
, which would be able to transform the givendocString
to the givenType
. Similarly to the way the@DefaultParameterTransformer
and other@Default...
transformers work. This is necessary in order to get pre-converted objects of thetargetType
in the test steps.✨ What's your proposed solution?
Add an option to define a
@DefaultDocTypeTransformer
for acontentType
.It would also be useful to be able to define, a fallback transformer, which is used if no transformers have been found for the corresponding
contentType
.⛏ Have you considered any alternatives or workarounds?
As of now it's necessary to define a
DocType
transformer for each of thetargetType
s, which leads to writing a lot of transformer methods with the same code.📚 Any additional context?
The way I see it is that the
DocStringTypeRegistryDocStringConverter
class will fallback to the@DefaultDocTypeTransformer(contentType = "X")
for the givencontentType
if it fails to find aDocString
transformer for the given pair of acontentType
andtargetType
. If there is no@DefaultDocTypeTransformer(contentType = "X")
for the givencontenType
then theDocStringTypeRegistryDocStringConverter
will fallback to the@DefaultDocTypeTransformer
, which can transform aDocString
with any givencontentType
to the giventargetType
.The text was updated successfully, but these errors were encountered: