Open-XML-SDK icon indicating copy to clipboard operation
Open-XML-SDK copied to clipboard

`SaveAs` also saves the original.

Open atrauzzi opened this issue 2 years ago • 5 comments

Description

When I open a document, make changes to some of its elements and later call SaveAs, the original source file also appears to be overwritten.

This happens even when I set AutoSave to false and does not happen prior to disposal.

Expected

My initial assumption was that calling SaveAs would result in the source file never being altered. Similar for example to how a save-as operation in an editor would work. There's some degree of surprise in the current behaviour.

atrauzzi avatar Mar 30 '22 17:03 atrauzzi

Hmm interesting. Do you have a repro available?

twsouthwick avatar Apr 12 '22 22:04 twsouthwick

Sorry, no -- very strapped for time at the moment.

atrauzzi avatar Apr 13 '22 15:04 atrauzzi

Yes, SaveAs does save the original. Unless deeper architectural changes are made, it must work that way. The main purpose is basically to create a copy, or clone, of the package at the given path, e.g., even if the package was opened on a MemoryStream.

Assuming you want to keep the original file unchanged, you'd have to read all bytes from that file, copy those to a MemoryStream, and later save that MemoryStream using SaveAs.

ThomasBarnekow avatar Apr 27 '22 16:04 ThomasBarnekow

Might be worth documenting as the mechanism of Saving As in any other context is to not overwrite the previously-saved path.

atrauzzi avatar Apr 28 '22 16:04 atrauzzi

@atrauzzi, yes, this would definitely warrant some explicit documentation or even a "warning". @AlfredHellstern, I'd say this is a "feature" and not a "bug". It might also be very complicated to do differently.

ThomasBarnekow avatar May 09 '22 15:05 ThomasBarnekow

Hello, are there any plans for this issue?

rstm-sf avatar Oct 16 '22 15:10 rstm-sf

As @ThomasBarnekow pointed out, this is how it was designed and would indeed take a much deeper architectural change to alter.

I agree, however, that this API is wrongly named. I'm all for suggestions and we can obsolete this one with a warning and provide an API that is better named.

Thoughts of the top of my head:

  • SaveAndCopyTo(...)
  • SaveAndClone(....)
  • CopyTo(....)

Other ideas?

twsouthwick avatar Nov 18 '22 19:11 twsouthwick

For v3.0, I'm opting to remove the API as-is since it is the same as clone and has unintuitive behavior (#1376). I'd be happy to reconsider a new behavior for this post v3.0 that doesn't have the current downfalls

twsouthwick avatar Apr 05 '23 16:04 twsouthwick