Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Encoding Java8 DateTime fails #98

Open
tonymurphy opened this issue Nov 22, 2020 · 0 comments
Open

Encoding Java8 DateTime fails #98

tonymurphy opened this issue Nov 22, 2020 · 0 comments

Comments

@tonymurphy
Copy link

tonymurphy commented Nov 22, 2020

Using OpenAPI maven generator and Feign, but the default encoder doesn't allow OffsetDateTime

Snippets from Open API contract:

/images: post: tags: - image summary: uploadImage operationId: uploadImage requestBody: $ref: '#/components/requestBodies/UploadImageRequest' responses: "201": $ref: '#/components/responses/ImagesResponse' default: $ref: '#/components/responses/ErrorResponse' requestBodies: UploadImageRequest: content: multipart/form-data: schema: $ref: '#/components/schemas/UploadImage' UploadImage: type: object properties: upload_date: type: string format: date-time image: type: string format: binary

I had to create this encoder as workaround to the problem

`import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;

import java.lang.reflect.Type;
import java.time.OffsetDateTime;

public class OffsetDateTimeAwareFormEncoder implements Encoder {

@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
    try {
        new Encoder.Default().encode(object, bodyType, template);
    } catch (EncodeException e) {
        if(bodyType == OffsetDateTime.class) {
            OffsetDateTime offsetDateTime = (OffsetDateTime)object;
            template.body(offsetDateTime.toString());
        } else {
            throw e;
        }
    }
}

}`

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

No branches or pull requests

1 participant