|
61 | 61 | import io.micronaut.inject.qualifiers.Qualifiers;
|
62 | 62 | import io.micronaut.jackson.ObjectMapperFactory;
|
63 | 63 | import io.micronaut.jackson.annotation.JacksonFeatures;
|
| 64 | +import io.micronaut.jackson.codec.JacksonMediaTypeCodec; |
64 | 65 | import io.micronaut.jackson.codec.JsonMediaTypeCodec;
|
65 | 66 | import io.micronaut.runtime.ApplicationConfiguration;
|
66 | 67 | import io.reactivex.Completable;
|
@@ -225,7 +226,7 @@ public Object intercept(MethodInvocationContext<Object, Object> context) {
|
225 | 226 | configuration.getParameters()
|
226 | 227 | .forEach(parameter -> queryParams.put(parameter, version));
|
227 | 228 | });
|
228 |
| - |
| 229 | + |
229 | 230 | Map<String, Object> attributes = new LinkedHashMap<>(ATTRIBUTES_INITIAL_CAPACITY);
|
230 | 231 |
|
231 | 232 | List<AnnotationValue<RequestAttribute>> attributeAnnotations = context.getAnnotationValuesByType(RequestAttribute.class);
|
@@ -646,11 +647,11 @@ private HttpClient getClient(MethodInvocationContext<Object, Object> context, An
|
646 | 647 |
|
647 | 648 | return clients.computeIfAbsent(clientKey, integer -> {
|
648 | 649 | HttpClient clientBean = beanContext.findBean(HttpClient.class, Qualifiers.byName(NameUtils.hyphenate(clientId))).orElse(null);
|
649 |
| - AnnotationValue<JacksonFeatures> jacksonFeatures = context.findAnnotation(JacksonFeatures.class).orElse(null); |
| 650 | + AnnotationValue<JacksonFeatures> jacksonFeaturesAnn = context.findAnnotation(JacksonFeatures.class).orElse(null); |
650 | 651 | Optional<Class<?>> configurationClass = clientAnn.classValue("configuration");
|
651 | 652 |
|
652 | 653 | if (null != clientBean) {
|
653 |
| - if (path == null && jacksonFeatures == null && !configurationClass.isPresent()) { |
| 654 | + if (path == null && jacksonFeaturesAnn == null && !configurationClass.isPresent()) { |
654 | 655 | return clientBean;
|
655 | 656 | }
|
656 | 657 | }
|
@@ -688,60 +689,70 @@ private HttpClient getClient(MethodInvocationContext<Object, Object> context, An
|
688 | 689 | DefaultHttpClient defaultClient = (DefaultHttpClient) client;
|
689 | 690 | defaultClient.setClientIdentifiers(clientId);
|
690 | 691 |
|
691 |
| - if (jacksonFeatures != null) { |
692 |
| - Optional<MediaTypeCodec> existingCodec = defaultClient.getMediaTypeCodecRegistry().findCodec(MediaType.APPLICATION_JSON_TYPE); |
693 |
| - ObjectMapper objectMapper = null; |
694 |
| - if (existingCodec.isPresent()) { |
695 |
| - MediaTypeCodec existing = existingCodec.get(); |
696 |
| - if (existing instanceof JsonMediaTypeCodec) { |
697 |
| - objectMapper = ((JsonMediaTypeCodec) existing).getObjectMapper().copy(); |
698 |
| - } |
699 |
| - } |
700 |
| - if (objectMapper == null) { |
701 |
| - objectMapper = new ObjectMapperFactory().objectMapper(null, null); |
702 |
| - } |
| 692 | + if (jacksonFeaturesAnn != null) { |
| 693 | + io.micronaut.jackson.codec.JacksonFeatures jacksonFeatures = new io.micronaut.jackson.codec.JacksonFeatures(); |
703 | 694 |
|
704 |
| - SerializationFeature[] enabledSerializationFeatures = jacksonFeatures.get("enabledSerializationFeatures", SerializationFeature[].class).orElse(null); |
| 695 | + |
| 696 | + SerializationFeature[] enabledSerializationFeatures = jacksonFeaturesAnn.get("enabledSerializationFeatures", SerializationFeature[].class).orElse(null); |
705 | 697 | if (enabledSerializationFeatures != null) {
|
706 | 698 | for (SerializationFeature serializationFeature : enabledSerializationFeatures) {
|
707 |
| - objectMapper.configure(serializationFeature, true); |
| 699 | + jacksonFeatures.addFeature(serializationFeature, true); |
708 | 700 | }
|
709 | 701 | }
|
710 | 702 |
|
711 |
| - DeserializationFeature[] enabledDeserializationFeatures = jacksonFeatures.get("enabledDeserializationFeatures", DeserializationFeature[].class).orElse(null); |
| 703 | + DeserializationFeature[] enabledDeserializationFeatures = jacksonFeaturesAnn.get("enabledDeserializationFeatures", DeserializationFeature[].class).orElse(null); |
712 | 704 |
|
713 | 705 | if (enabledDeserializationFeatures != null) {
|
714 |
| - for (DeserializationFeature serializationFeature : enabledDeserializationFeatures) { |
715 |
| - objectMapper.configure(serializationFeature, true); |
| 706 | + for (DeserializationFeature deserializationFeature : enabledDeserializationFeatures) { |
| 707 | + jacksonFeatures.addFeature(deserializationFeature, true); |
716 | 708 | }
|
717 | 709 | }
|
718 | 710 |
|
719 |
| - SerializationFeature[] disabledSerializationFeatures = jacksonFeatures.get("disabledSerializationFeatures", SerializationFeature[].class).orElse(null); |
| 711 | + SerializationFeature[] disabledSerializationFeatures = jacksonFeaturesAnn.get("disabledSerializationFeatures", SerializationFeature[].class).orElse(null); |
720 | 712 | if (disabledSerializationFeatures != null) {
|
721 | 713 | for (SerializationFeature serializationFeature : disabledSerializationFeatures) {
|
722 |
| - objectMapper.configure(serializationFeature, false); |
| 714 | + jacksonFeatures.addFeature(serializationFeature, false); |
723 | 715 | }
|
724 | 716 | }
|
725 | 717 |
|
726 |
| - DeserializationFeature[] disabledDeserializationFeatures = jacksonFeatures.get("disabledDeserializationFeatures", DeserializationFeature[].class).orElse(null); |
| 718 | + DeserializationFeature[] disabledDeserializationFeatures = jacksonFeaturesAnn.get("disabledDeserializationFeatures", DeserializationFeature[].class).orElse(null); |
727 | 719 |
|
728 | 720 | if (disabledDeserializationFeatures != null) {
|
729 | 721 | for (DeserializationFeature feature : disabledDeserializationFeatures) {
|
730 |
| - objectMapper.configure(feature, false); |
| 722 | + jacksonFeatures.addFeature(feature, false); |
731 | 723 | }
|
732 | 724 | }
|
733 | 725 |
|
734 |
| - defaultClient.setMediaTypeCodecRegistry( |
735 |
| - MediaTypeCodecRegistry.of( |
736 |
| - new JsonMediaTypeCodec(objectMapper, |
737 |
| - beanContext.getBean(ApplicationConfiguration.class), |
738 |
| - beanContext.findBean(CodecConfiguration.class, Qualifiers.byName(JsonMediaTypeCodec.CONFIGURATION_QUALIFIER)).orElse(null)))); |
| 726 | + List<MediaTypeCodec> codecs = new ArrayList<>(2); |
| 727 | + MediaTypeCodecRegistry codecRegistry = defaultClient.getMediaTypeCodecRegistry(); |
| 728 | + for (MediaTypeCodec codec: codecRegistry.getCodecs()) { |
| 729 | + if (codec instanceof JacksonMediaTypeCodec) { |
| 730 | + codecs.add(((JacksonMediaTypeCodec) codec).cloneWithFeatures(jacksonFeatures)); |
| 731 | + } else { |
| 732 | + codecs.add(codec); |
| 733 | + } |
| 734 | + } |
| 735 | + if (!codecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE).isPresent()) { |
| 736 | + codecs.add(createNewJsonCodec(beanContext, jacksonFeatures)); |
| 737 | + } |
| 738 | + defaultClient.setMediaTypeCodecRegistry(MediaTypeCodecRegistry.of(codecs)); |
739 | 739 | }
|
740 | 740 | }
|
741 | 741 | return client;
|
742 | 742 | });
|
743 | 743 | }
|
744 | 744 |
|
| 745 | + private static MediaTypeCodec createNewJsonCodec(BeanContext beanContext, io.micronaut.jackson.codec.JacksonFeatures jacksonFeatures) { |
| 746 | + ObjectMapper objectMapper = new ObjectMapperFactory().objectMapper(null, null); |
| 747 | + |
| 748 | + jacksonFeatures.getDeserializationFeatures().forEach(objectMapper::configure); |
| 749 | + jacksonFeatures.getSerializationFeatures().forEach(objectMapper::configure); |
| 750 | + |
| 751 | + return new JsonMediaTypeCodec(objectMapper, |
| 752 | + beanContext.getBean(ApplicationConfiguration.class), |
| 753 | + beanContext.findBean(CodecConfiguration.class, Qualifiers.byName(JsonMediaTypeCodec.CONFIGURATION_QUALIFIER)).orElse(null)); |
| 754 | + } |
| 755 | + |
745 | 756 | private String getClientId(AnnotationValue<Client> clientAnn) {
|
746 | 757 | String clientId = clientAnn.stringValue().orElse(null);
|
747 | 758 | if (clientId == null) {
|
|
0 commit comments