Skip to content

Commit ec51b4f

Browse files
Fix LT-22159: Fix crash when entry has both affix and stem allomorphs
1 parent 62c290f commit ec51b4f

3 files changed

Lines changed: 41 additions & 16 deletions

File tree

Src/xWorks/ConfiguredLcmGenerator.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,19 @@ private static bool GetPropValueForModelField(object fieldOwner, ConfigurableDic
744744
}
745745
// Convert the contents to IEnumerable<T>
746746
var objects = contents.Select(id => cache.LangProject.Services.GetObject(id));
747-
var type = objects.FirstOrDefault()?.GetType() ?? typeof(object);
748-
var castMethod = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(type);
749-
propertyValue = castMethod.Invoke(null, new object[] { objects });
747+
var firstObjType = objects.FirstOrDefault()?.GetType();
748+
// check that each item in objects can be cast to firstObjType
749+
if (firstObjType != null && objects.All(o => firstObjType.IsInstanceOfType(o)))
750+
{
751+
var castMethod = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(firstObjType);
752+
propertyValue = castMethod.Invoke(null, new object[] { objects });
753+
}
754+
else
755+
{
756+
var castMethod = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(typeof(object));
757+
propertyValue = castMethod.Invoke(null, new object[] { objects });
758+
759+
}
750760
break;
751761
}
752762
case (int)CellarPropertyType.ReferenceAtomic:
@@ -1575,7 +1585,7 @@ private static IFragment GenerateContentForCollection(object collectionField, Li
15751585
else
15761586
{
15771587
bool first = true;
1578-
foreach (var item in collection)
1588+
foreach (object item in collection)
15791589
{
15801590
frag.Append(GenerateCollectionItemContent(nodeList, pubDecorator, item, collectionOwner, settings, first));
15811591
first = false;

Src/xWorks/xWorksTests/ConfiguredXHTMLGeneratorTestHelpers.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,21 +430,33 @@ internal static ILexExampleSentence AddExampleToSense(ILexSense sense, string co
430430
return example;
431431
}
432432

433-
private IMoForm AddAllomorphToEntry(ILexEntry entry)
433+
private IMoForm AddAllomorphToEntry(ILexEntry entry, bool stem = true)
434434
{
435-
var morphFact = Cache.ServiceLocator.GetInstance<IMoStemAllomorphFactory>();
436-
var morph = morphFact.Create();
437-
entry.AlternateFormsOS.Add(morph);
438-
morph.Form.set_String(m_wsFr, TsStringUtils.MakeString("Allomorph", m_wsFr));
439-
440-
// add environment to the allomorph
441435
const int stringRepresentationFlid = 5097008;
442436
var env = Cache.ServiceLocator.GetInstance<IPhEnvironmentFactory>().Create();
443437
Cache.LangProject.PhonologicalDataOA.EnvironmentsOS.Add(env);
444-
morph.PhoneEnvRC.Add(env);
445438
Cache.MainCacheAccessor.SetString(env.Hvo, stringRepresentationFlid, TsStringUtils.MakeString("phoneyEnv", m_wsEn));
446-
447-
return morph;
439+
if (stem)
440+
{
441+
var morphFact = Cache.ServiceLocator.GetInstance<IMoStemAllomorphFactory>();
442+
var morph = morphFact.Create();
443+
entry.AlternateFormsOS.Add(morph);
444+
morph.Form.set_String(m_wsFr, TsStringUtils.MakeString("StemAllomorph", m_wsFr));
445+
// add environment to the stem allomorph (PhoneEnvRC is not in the common base class between stem and affix allomorphs)
446+
morph.PhoneEnvRC.Add(env);
447+
return morph;
448+
}
449+
else
450+
{
451+
// if not stem, then it is an affix allomorph
452+
var morphFact = Cache.ServiceLocator.GetInstance<IMoAffixAllomorphFactory>();
453+
var morph = morphFact.Create();
454+
entry.AlternateFormsOS.Add(morph);
455+
morph.Form.set_String(m_wsFr, TsStringUtils.MakeString("AffixAllomorph", m_wsFr));
456+
// add environment to the allomorph
457+
morph.PhoneEnvRC.Add(env);
458+
return morph;
459+
}
448460
}
449461

450462
internal static ICmPicture CreatePicture(LcmCache cache, bool exists = true, string caption = "caption", string ws = "en")

Src/xWorks/xWorksTests/ConfiguredXHTMLGeneratorTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3580,15 +3580,18 @@ public void GenerateContentForEntry_EnvironmentsAndAllomorphsAreGenerated()
35803580

35813581
var mainEntry = CreateInterestingLexEntry(Cache);
35823582
AddAllomorphToEntry(mainEntry);
3583+
AddAllomorphToEntry(mainEntry, false);
35833584

35843585
var settings = new ConfiguredLcmGenerator.GeneratorSettings(Cache, m_propertyTable, false, false, null);
35853586
//SUT
35863587
var result = ConfiguredLcmGenerator.GenerateContentForEntry(mainEntry, mainEntryNode, null, settings).ToString();
35873588
const string xPathThruAllomorph = "/div[@class='lexentry']/span[@class='alternateformsos']/span[@class='alternateformso']";
35883589
AssertThatXmlIn.String(result).HasSpecifiedNumberOfMatchesForXpath(
3589-
xPathThruAllomorph + "/span[@class='form']/span[@lang='fr' and text()='Allomorph']", 1);
3590+
xPathThruAllomorph + "/span[@class='form']/span[@lang='fr' and text()='StemAllomorph']", 1);
3591+
AssertThatXmlIn.String(result).HasSpecifiedNumberOfMatchesForXpath(
3592+
xPathThruAllomorph + "/span[@class='form']/span[@lang='fr' and text()='AffixAllomorph']", 1);
35903593
AssertThatXmlIn.String(result).HasSpecifiedNumberOfMatchesForXpath(xPathThruAllomorph +
3591-
"/span[@class='allomorphenvironments']/span[@class='allomorphenvironment']/span[@class='stringrepresentation']/span[@lang='en' and text()='phoneyEnv']", 1);
3594+
"/span[@class='allomorphenvironments']/span[@class='allomorphenvironment']/span[@class='stringrepresentation']/span[@lang='en' and text()='phoneyEnv']", 2);
35923595
}
35933596

35943597
[Test]

0 commit comments

Comments
 (0)