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

Fix bug in SubmodelService: The Call of GetSubmodelMeta Changes The InMemory Storage Object #502

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
mateusmolina-iese marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.eclipse.digitaltwin.basyx.core.exceptions;

public class FailedToDeepCopyException extends RuntimeException {

public FailedToDeepCopyException(String objId, Throwable e) {
super(getMessage(objId), e);
}

private static String getMessage(String objId) {
return "Failed to deep copy object with id " + objId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.DeserializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.SerializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonDeserializer;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer;
import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
Expand All @@ -54,6 +50,7 @@
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationSupport;
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncoder;
import org.eclipse.digitaltwin.basyx.serialization.SubmodelMetadataUtil;
import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository;
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelService;
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelServiceFactory;
Expand Down Expand Up @@ -255,7 +252,7 @@ public Submodel getSubmodelByIdMetadata(String submodelId) throws ElementDoesNot

Submodel submodel = getSubmodel(submodelId);

return getSubmodelDeepCopy(submodel);
return SubmodelMetadataUtil.extractMetadata(submodel);
}

@Override
Expand Down Expand Up @@ -303,23 +300,6 @@ private void throwIfMissingId(Collection<Submodel> submodels) {
submodels.stream().map(Submodel::getId).forEach(this::throwIfSubmodelIdEmptyOrNull);
}

private Submodel getSubmodelDeepCopy(Submodel submodel) {

try {
String submodelAsJSON = new JsonSerializer().write(submodel);

Submodel submodelDeepCopy = new JsonDeserializer().read(submodelAsJSON, Submodel.class);

submodelDeepCopy.setSubmodelElements(null);

return submodelDeepCopy;
} catch (DeserializationException e) {
throw new RuntimeException("Unable to deserialize the Submodel", e);
} catch (SerializationException e) {
throw new RuntimeException("Unable to serialize the Submodel", e);
}
}

private SubmodelService getSubmodelServiceOrThrow(String submodelId) {
Submodel submodel = submodelBackend.findById(submodelId).orElseThrow(() -> new ElementDoesNotExistException(submodelId));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.serialization;

import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.DeserializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.SerializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonDeserializer;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
import org.eclipse.digitaltwin.basyx.core.exceptions.FailedToDeepCopyException;

public final class SubmodelMetadataUtil {

private SubmodelMetadataUtil() {
}

/**
* Returns a new submodel that only contains the metadata of the given submodel.
*
* @param submodel
* @return A new submodel with the metadata of the given submodel.
*/
public static Submodel extractMetadata(Submodel submodel) {
Submodel submodelDeepCopy = deepCopy(submodel);

submodelDeepCopy.setSubmodelElements(null);

return submodelDeepCopy;
}

/**
* Deep copy a submodel.
*
* @param submodel
* @return A deep copy of the submodel.
*
* @throws FailedToDeepCopyException
*
*/
public static Submodel deepCopy(Submodel submodel) {
mateusmolina-iese marked this conversation as resolved.
Show resolved Hide resolved
try {
String submodelAsJSON = new JsonSerializer().write(submodel);

return new JsonDeserializer().read(submodelAsJSON, Submodel.class);

} catch (DeserializationException | SerializationException e) {
throw new FailedToDeepCopyException(submodel.getId(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eclipse.digitaltwin.basyx.http.pagination.PagedResult;
import org.eclipse.digitaltwin.basyx.http.pagination.PagedResultPagingMetadata;
import org.eclipse.digitaltwin.basyx.pagination.GetSubmodelElementsResult;
import org.eclipse.digitaltwin.basyx.serialization.SubmodelMetadataUtil;
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelService;
import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelElementValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelValueOnly;
Expand Down Expand Up @@ -164,9 +165,8 @@ public ResponseEntity<Submodel> getSubmodelMetadata(@Parameter(in = ParameterIn.
"core" }, defaultValue = "deep")) @Valid @RequestParam(value = "level", required = false, defaultValue = "deep") String level) {

Submodel submodel = service.getSubmodel();
submodel.setSubmodelElements(null);

return new ResponseEntity<Submodel>(submodel, HttpStatus.OK);
return new ResponseEntity<>(SubmodelMetadataUtil.extractMetadata(submodel), HttpStatus.OK);
}

@Override
Expand Down Expand Up @@ -320,5 +320,4 @@ private void closeInputStream(InputStream fileInputstream) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@

package org.eclipse.digitaltwin.basyx.submodelservice.http;

import static org.junit.Assert.assertEquals;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.ParseException;
import org.eclipse.digitaltwin.basyx.http.serialization.BaSyxHttpTestUtils;
import org.junit.Test;
import org.springframework.http.HttpStatus;

/**
* Base testsuite for all Submodel Service HTTP tests related to the Submodel
Expand Down Expand Up @@ -63,6 +66,19 @@ public void getSubmodelMetadata() throws IOException, ParseException {
BaSyxHttpTestUtils.assertSameJSONContent(expected, submodelMetadata);
}

@Test
public void getSubmodelMetadata_preservesSubmodel() throws IOException, ParseException {

String submodelBefore = BaSyxHttpTestUtils.getResponseAsString(BaSyxHttpTestUtils.executeGetOnURL(getURL()));

CloseableHttpResponse response = BaSyxHttpTestUtils.executeGetOnURL(getSubmodelMetadataURL());
assertEquals(HttpStatus.OK.value(), response.getCode());

String submodelAfter = BaSyxHttpTestUtils.getResponseAsString(BaSyxHttpTestUtils.executeGetOnURL(getURL()));

BaSyxHttpTestUtils.assertSameJSONContent(submodelBefore, submodelAfter);
}

private String getSubmodelMetadataURL() {
return getURL() + "/$metadata";
}
Expand Down