{"action":"created","issue":{"url":"https://api.github.com/repos/cucumber/cucumber-jvm/issues/308","labels_url":"https://api.github.com/repos/cucumber/cucumber-jvm/issues/308/labels{/name}","comments_url":"https://api.github.com/repos/cucumber/cucumber-jvm/issues/308/comments","events_url":"https://api.github.com/repos/cucumber/cucumber-jvm/issues/308/events","html_url":"https://github.com/cucumber/cucumber-jvm/issues/308","id":4298286,"number":308,"title":"Allow specification of default converters to be used in StepDefs","user":{"login":"tknerr","id":365744,"avatar_url":"https://avatars.githubusercontent.com/u/365744?v=3","gravatar_id":"","url":"https://api.github.com/users/tknerr","html_url":"https://github.com/tknerr","followers_url":"https://api.github.com/users/tknerr/followers","following_url":"https://api.github.com/users/tknerr/following{/other_user}","gists_url":"https://api.github.com/users/tknerr/gists{/gist_id}","starred_url":"https://api.github.com/users/tknerr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tknerr/subscriptions","organizations_url":"https://api.github.com/users/tknerr/orgs","repos_url":"https://api.github.com/users/tknerr/repos","events_url":"https://api.github.com/users/tknerr/events{/privacy}","received_events_url":"https://api.github.com/users/tknerr/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/cucumber/cucumber-jvm/labels/Core","name":"Core","color":"02d7e1"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2012-04-26T09:18:44Z","updated_at":"2015-12-01T03:09:38Z","closed_at":null,"body":"As per cucumber-jvm 1.0.4 you can specify XStreamConverters for both arguments in data tables and regular arguments.\r\n\r\nFor data table arguments:\r\n```java\r\n@Given(\"^I have some stuff in a data table:$\")\r\npublic void I_have_some_stuff_in_a_data_table(List<Stuff> stuffs) {\r\n     //...\r\n}\r\n\r\npublic static class Stuff {\r\n    @XStreamConverter(FooConverter.class)\r\n    public Foo foo;\r\n}\r\n```\r\n\r\nFor regular arguments:\r\n```java\r\n@Given(\"^I have some foo named \\\"([^\\\"]*)\\\"$\")\r\npublic void I_have_some_foo_named(Foo foo) {\r\n    //...\r\n}\r\n\r\n@XStreamConverter(FooConverter.class)\r\npublic static class Foo{\r\n    //...\r\n}\r\n```\r\n\r\nIt would be great if we could specify what the default XStreamConverters for specific types are, which would have the additional benefits of:\r\n - don't need to repeat yourself that Foo should be converter using FooConverter across multiple places\r\n - would allow to specify converters for runtime classes which you could not annotate otherwise because they are not locally defined in your StepDefs\r\n\r\nI could imagine something like this (outlined here: https://gist.github.com/2497953):\r\n```java\r\n@StepDefOptions(\r\n\t\tdefaultConverters = {\r\n\t\t\t\t@Mapping(type = Foo.class, converter = FooConverter.class),\r\n\t\t\t\t@Mapping(type = Bag.class, converter = BarConverter.class),\r\n\t\t\t\t@Mapping(type = Baz.class, converter = BazConverter.class),\r\n\t\t})\r\npublic class FooStepDefs {\r\n    //...\r\n}\r\n```\r\n\r\nor this (outlined here: https://gist.github.com/2488376):\r\n```java\r\n@StepDefOptions(defaultConverters = MyConverters.class)\r\npublic class FooStepDefs {\r\n\t\r\n\tstatic class MyConverters extends MapBasedConverterRegistry {\r\n\t\tpublic MyConverters(Map<Class<?>, Class<? extends ConverterMatcher>> converterRegistryMap) {\r\n\t\t\tsuper(new ConverterMapBuilder()\r\n\t\t\t\t\t.convert(Foo.class).with(FooConverter.class)\r\n\t\t\t\t\t.convert(Bar.class).with(BarConverter.class)\r\n\t\t\t\t\t.convert(Baz.class).with(BazConverter.class)\r\n\t\t\t\t\t.build());\r\n\t\t}\r\n\t}\r\n\r\n    //...\r\n```\r\n\r\n\r\n\r\nNot sure whether default converters should be defineable on StepDef level, Test level (e.g. in @Cucumber.Options) or even more globally. \r\n\r\nWhat do you think?"},"comment":{"url":"https://api.github.com/repos/cucumber/cucumber-jvm/issues/comments/160835927","html_url":"https://github.com/cucumber/cucumber-jvm/issues/308#issuecomment-160835927","issue_url":"https://api.github.com/repos/cucumber/cucumber-jvm/issues/308","id":160835927,"user":{"login":"Darkvater","id":2002972,"avatar_url":"https://avatars.githubusercontent.com/u/2002972?v=3","gravatar_id":"","url":"https://api.github.com/users/Darkvater","html_url":"https://github.com/Darkvater","followers_url":"https://api.github.com/users/Darkvater/followers","following_url":"https://api.github.com/users/Darkvater/following{/other_user}","gists_url":"https://api.github.com/users/Darkvater/gists{/gist_id}","starred_url":"https://api.github.com/users/Darkvater/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Darkvater/subscriptions","organizations_url":"https://api.github.com/users/Darkvater/orgs","repos_url":"https://api.github.com/users/Darkvater/repos","events_url":"https://api.github.com/users/Darkvater/events{/privacy}","received_events_url":"https://api.github.com/users/Darkvater/received_events","type":"User","site_admin":false},"created_at":"2015-12-01T03:09:38Z","updated_at":"2015-12-01T03:09:38Z","body":"I would like to second this as well. The current approach is undesirable as everywhere the custom converted type is used will have to be annotated. You want to set this globally.\r\n\r\n \r\n\r\nTake for example org.joda.DateTime. This can be marshalled currently through the DynamicClassWithStringAssignableConverter, however new DateTime(\"string\") will lose the timezone information so you want to use DateTime.parse(). Currently there is no official way of adding converters to cucumber.\r\n\r\n \r\n\r\nI have hacked deep into cucumber-jvm and made it work as follows, which is okay for now:\r\n\r\nGroovyBackend.getInstance().glue.localizedXStreams.get(Locale.ENGLISH).registerConverter(converter)\r\n\r\n \r\n\r\nNow obviously this is not ideal and I wish there was an official way of doing this. The ConverterRegistry is a good start but it needs to be exposed."}}