From e5ba95d7a652bbf5e843d61e08b94de25487875a Mon Sep 17 00:00:00 2001 From: Michael Jacoby Date: Tue, 10 Dec 2024 15:26:49 +0100 Subject: [PATCH] fix response serialization of /description in HTTP endpoint --- .../serialization/HttpJsonApiSerializer.java | 5 ++ .../ServiceDescriptionSerializer.java | 54 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 endpoint/http/src/main/java/de/fraunhofer/iosb/ilt/faaast/service/endpoint/http/serialization/ServiceDescriptionSerializer.java diff --git a/endpoint/http/src/main/java/de/fraunhofer/iosb/ilt/faaast/service/endpoint/http/serialization/HttpJsonApiSerializer.java b/endpoint/http/src/main/java/de/fraunhofer/iosb/ilt/faaast/service/endpoint/http/serialization/HttpJsonApiSerializer.java index ad7675d04..9328ca311 100644 --- a/endpoint/http/src/main/java/de/fraunhofer/iosb/ilt/faaast/service/endpoint/http/serialization/HttpJsonApiSerializer.java +++ b/endpoint/http/src/main/java/de/fraunhofer/iosb/ilt/faaast/service/endpoint/http/serialization/HttpJsonApiSerializer.java @@ -16,8 +16,10 @@ import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.util.StdDateFormat; import de.fraunhofer.iosb.ilt.faaast.service.dataformat.json.JsonApiSerializer; +import de.fraunhofer.iosb.ilt.faaast.service.model.ServiceDescription; /** @@ -30,5 +32,8 @@ protected void modifyMapper(JsonMapper mapper) { super.modifyMapper(mapper); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true)); + SimpleModule module = new SimpleModule(); + module.addSerializer(ServiceDescription.class, new ServiceDescriptionSerializer()); + mapper.registerModule(module); } } diff --git a/endpoint/http/src/main/java/de/fraunhofer/iosb/ilt/faaast/service/endpoint/http/serialization/ServiceDescriptionSerializer.java b/endpoint/http/src/main/java/de/fraunhofer/iosb/ilt/faaast/service/endpoint/http/serialization/ServiceDescriptionSerializer.java new file mode 100644 index 000000000..5a20418e6 --- /dev/null +++ b/endpoint/http/src/main/java/de/fraunhofer/iosb/ilt/faaast/service/endpoint/http/serialization/ServiceDescriptionSerializer.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 Fraunhofer IOSB, eine rechtlich nicht selbstaendige + * Einrichtung der Fraunhofer-Gesellschaft zur Foerderung der angewandten + * Forschung e.V. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.serialization; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import de.fraunhofer.iosb.ilt.faaast.service.model.ServiceDescription; +import de.fraunhofer.iosb.ilt.faaast.service.util.LambdaExceptionHelper; +import java.io.IOException; +import java.util.Objects; + + +/** + * Serializer for {@link de.fraunhofer.iosb.ilt.faaast.service.model.ServiceDescription}. + */ +public class ServiceDescriptionSerializer extends StdSerializer { + + private static final String FIELD_PROFILES = "profiles"; + + public ServiceDescriptionSerializer() { + this(null); + } + + + public ServiceDescriptionSerializer(Class t) { + super(t); + } + + + @Override + public void serialize(ServiceDescription value, JsonGenerator generator, SerializerProvider provider) throws IOException { + generator.writeStartObject(); + generator.writeFieldName(FIELD_PROFILES); + generator.writeStartArray(); + if (Objects.nonNull(value) && Objects.nonNull(value.getProfiles())) { + value.getProfiles().forEach(LambdaExceptionHelper.rethrowConsumer(x -> generator.writeString(x.getId()))); + } + generator.writeEndArray(); + generator.writeEndObject(); + } +}