Skip to content
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

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant