ScaCap / spring-auto-restdocs

Spring Auto REST Docs is an extension to Spring REST Docs
https://scacap.github.io/spring-auto-restdocs/
Apache License 2.0
311 stars 86 forks source link

Make translation configurable #318

Closed MikeSafonov closed 5 years ago

MikeSafonov commented 5 years ago

Problem: I want to provide same custom snippet translation to several applications. I need to copy paste SnippetMessages.properties with same translation at every source code.

To solve this problem i want to create a starter with default spring-restdocs/spring-auto-restdocs configuration and custom translation. Unfortunately, I can’t control the behavior of the SnippetTranslationResolver.

Solution:

jmisur commented 5 years ago

Hi, before we merge #329, did you try to reuse SnippetMessages.properties via shared library? I guess resource should be possible to find anywhere on the classpath wherever it comes from. How are you going to register your translation resolver using spring boot starter?

MikeSafonov commented 5 years ago

Hello. Of course, you are right, but properties file comes with some encoding issues. For example my SnippetMessages.properties file looks like this:

authorization=\u0410\u0432\u0442\u043E\u0440\u0438\u0437\u0430\u0446\u0438\u044F

After #329 will be merged, I will be able to store my translations anywhere: source code / json, properties files / access via HTTP API / something else. And most importantly, I will be able to control the format and encoding of translations by myself.

As for the starter, I could suggest the following options :

  1. Starter comes with custom implementation of SnippetTranslationResolver / ConstraintDescriptionResolver, mockMvc creation and configuration will be on client side.
  2. Starter comes with already created and configured mockMvc using abstract EndpoitTest. Some code concept:

    public abstract class EndpointTest {
    
    @Autowired
    protected MockMvc mockMvc;
    @Autowired
    protected ObjectMapper objectMapper;
    
    @BeforeEach
    void setUp(WebApplicationContext context,
               RestDocumentationContextProvider restDocumentation) {
        SnippetTranslationResolver translationResolver = new JsonSnippetTranslationResolver();
        ConstraintDescriptionResolver constraintDescriptionResolver = new InMemoryConstraintDescriptionResolver();
         this.mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .alwaysDo(JacksonResultHandlers.prepareJackson(objectMapper, translationResolver, constraintDescriptionResolver ))
         ...
    }
    }
jmisur commented 5 years ago

Ok sounds good! I will merge your PR. When you're ready with your part of the code and there are no fixes necessary for you on SARD side, we can release it officially with some docs.

MikeSafonov commented 5 years ago

Hi! I wrote a test starter and I have no fixes or improvements. I think you could make a release.

MikeSafonov commented 5 years ago

Hello. Any plans to make new release?

jmisur commented 5 years ago

Hi, we released 2.0.6, it should be on mvn central shortly.

MikeSafonov commented 5 years ago

Thanks!

jmisur commented 5 years ago

I guess this can be closed.