diff --git a/compiler2/union.c b/compiler2/union.c
index a397195daff6ee404bf0653979ba005c21c492ad..8ee0cbf2b54902af9f98e6f848da755530c65052 100644
--- a/compiler2/union.c
+++ b/compiler2/union.c
@@ -2012,7 +2012,7 @@ void defUnionClass(struct_def const *sdef, output_struct *output)
       }
       if (!sdef->isOptional) {
         src = mputstr(src,
-          "    else {\n"
+          "    else if (!(p_flavor & XER_OPTIONAL)) {\n"
           "      ec_1.set_msg(\" \");\n"
           "      TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG, "
           "\"'%s' does not match any alternative\", elem_name);\n"
diff --git a/regression_test/XML/Makefile b/regression_test/XML/Makefile
index 43c226eaf95b14a127beed749473efa9d77e9de4..10dbffeee06f14887401501999120e82c0808a06 100644
--- a/regression_test/XML/Makefile
+++ b/regression_test/XML/Makefile
@@ -30,7 +30,8 @@ endif
 XDIRS := $(wildcard $(SHADOWED)) xsdConverter \
 HM60295 HN15589 HQ30408 HR49727 HU13380 $(RT2_ONLY) \
 XmlWorkflow tpdValidTest AbstractBlock UseNilLong AttributeFormDefault \
-RecordOmit XSDBaseType LegacyUntaggedUnion Printing Bug570707
+RecordOmit XSDBaseType LegacyUntaggedUnion Printing Bug570707 \
+Issue545 Issue585
 
 # List of fake targets:
 .PHONY: all dep clean run $(XDIRS) $(addsuffix /, $(XDIRS)) profile