Skip to content

Commit 4ec5720

Browse files
authored
Use default document type if no main part is available (dotnet#1836)
Fixes dotnet#1745
1 parent 6c85463 commit 4ec5720

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.Designer.cs

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,6 @@
240240
<data name="MultipleRelationshipsToSamePart" xml:space="preserve">
241241
<value>There are more than one relationship references that target the specified part.</value>
242242
</data>
243-
<data name="NoMainPart" xml:space="preserve">
244-
<value>The specified package is invalid. The main part is missing.</value>
245-
</data>
246243
<data name="NonCompositeNoChild" xml:space="preserve">
247244
<value>Non-composite elements do not have child elements.</value>
248245
</data>

src/DocumentFormat.OpenXml/Packaging/TypedPackageFeatureCollection.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ private TDocumentType EnsureDocumentType()
3939
return existing;
4040
}
4141

42-
var hasRelationships = false;
4342
var package = this.GetRequired<IPackageFeature>().Package;
4443

4544
foreach (var relationship in package.Relationships)
4645
{
47-
hasRelationships = true;
4846
if (relationship.RelationshipType == RelationshipType)
4947
{
5048
var uriTarget = PackUriHelper.ResolvePartUri(OpenXmlPackage.Uri, relationship.TargetUri);
@@ -55,14 +53,10 @@ private TDocumentType EnsureDocumentType()
5553
}
5654
}
5755

58-
if (!hasRelationships)
56+
// If document type hasn't been set by content, start with the TDocumentType default value
57+
if (!_documentType.HasValue)
5958
{
60-
_documentType = default;
61-
}
62-
63-
if (_documentType is null)
64-
{
65-
throw new OpenXmlPackageException(ExceptionMessages.NoMainPart);
59+
_documentType = default(TDocumentType);
6660
}
6761

6862
return _documentType.Value;

test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlPackageTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,26 @@ public void PresentationDocumentTypeIsRetrievedIfAvailable()
570570
Assert.Equal(PresentationDocumentType.Template, doc.DocumentType);
571571
}
572572

573+
[Fact]
574+
public void DocumentTypeUsesDefault()
575+
{
576+
// Arrange
577+
using var ms = new MemoryStream();
578+
579+
// Note - if a main part is not created, then no type is persisted
580+
using (var emptySpreadsheet = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.MacroEnabledTemplate))
581+
{
582+
}
583+
584+
ms.Position = 0;
585+
586+
// Assert/Act
587+
using (var result = SpreadsheetDocument.Open(ms, true))
588+
{
589+
Assert.Equal(default, result.DocumentType);
590+
}
591+
}
592+
573593
/// <summary>
574594
/// ChangeDocumentTypeInternalTest.
575595
/// </summary>

0 commit comments

Comments
 (0)