Skip to content

Commit

Permalink
fix: SubmodelService: bug getSubmodelMetadata changes submodel
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusmolina-iese committed Oct 24, 2024
1 parent c81ea9c commit a04e1b0
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
import java.util.Arrays;
import java.util.List;

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.OperationRequest;
import org.eclipse.digitaltwin.aas4j.v3.model.OperationResult;
import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable;
Expand Down Expand Up @@ -164,9 +168,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<Submodel>(getSubmodelDeepCopy(submodel), HttpStatus.OK);
}

@Override
Expand Down Expand Up @@ -321,4 +324,20 @@ private void closeInputStream(InputStream fileInputstream) {
}
}

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);
}
}
}

0 comments on commit a04e1b0

Please sign in to comment.