Skip to content

Commit

Permalink
fixup! Add support for escalating SDWs to Errors
Browse files Browse the repository at this point in the history
- add new SchemaDefinitionErrorFromWarning class that extends SDW so any changes to SDW in the future will be automatically inherited
- add logic to ProcessorStateBases.scala as well
- add test to demonstrate tunable ignored when warning is suppressed
- add test to demonstrate runtime SDW can be escalated as well

DAFFODIL-2810
  • Loading branch information
olabusayoT committed Sep 27, 2024
1 parent 32be587 commit 73088d3
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ class SchemaDefinitionError(

}

class SchemaDefinitionErrorFromWarning(
sdw: SchemaDefinitionDiagnosticBase,
warnID: WarnID
) extends SchemaDefinitionWarning(
warnID,
sdw.sc.toScalaOption,
sdw.annotationContext.toScalaOption,
({
val origMsg = if (sdw.mfmt.isDefined) sdw.mfmt.get else ""
val msg = s"warning escalated to error: ${warnID}. " + origMsg
msg
}),
sdw.args: _*
) {

override def isError = true
override def modeName = super.modeName + " Warning Escalated"

}

/**
* Specific class used for this specific error, because we need to pick this off
* in the debugger for special handling.
Expand Down Expand Up @@ -191,12 +211,12 @@ final class TunableLimitExceededError(
}

abstract class SchemaDefinitionDiagnosticBase(
sc: Maybe[SchemaFileLocation],
val sc: Maybe[SchemaFileLocation],
runtimeContext: Maybe[ParseOrUnparseState],
private val annotationContext: Option[SchemaFileLocation],
val annotationContext: Option[SchemaFileLocation],
mc: Maybe[Throwable],
mfmt: Maybe[String],
args: Any*
val mfmt: Maybe[String],
val args: Any*
) extends Diagnostic(
sc,
if (runtimeContext.isDefined) Maybe(runtimeContext.get.currentLocation) else Nope,
Expand Down Expand Up @@ -262,24 +282,20 @@ trait ImplementsThrowsOrSavesSDE extends ImplementsThrowsSDE with SavesErrorsAnd
val suppress = lssdw.contains(warnID) || lssdw.contains(WarnID.All) ||
tssdw.contains(warnID) || tssdw.contains(WarnID.All)
if (!suppress) {
val sdw = new SchemaDefinitionWarning(
warnID,
Some(schemaFileLocation),
NoAnnotationContext,
fmt,
args: _*
)
if (tunable.escalateWarningsToErrors) {
val msg = "warnings escalated to errors: " + fmt
val sde = new SchemaDefinitionError(
Some(schemaFileLocation),
NoAnnotationContext,
msg,
args: _*
val sde = new SchemaDefinitionErrorFromWarning(
sdw,
warnID
)
error(sde)
toss(sde)
} else {

val sdw = new SchemaDefinitionWarning(
warnID,
Some(schemaFileLocation),
NoAnnotationContext,
fmt,
args: _*
)
warn(sdw)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ class DataProcessor(
state.dataInputStream.inputSource.setInvalid
state.setFailed(rsde)
}
case sdefw: SchemaDefinitionErrorFromWarning => {
state.dataInputStream.inputSource.setInvalid
state.setFailed(sdefw)
}
case e: ErrorAlreadyHandled => {
state.setFailed(e.th)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import org.apache.daffodil.runtime1.dpath.DState
import org.apache.daffodil.runtime1.dsom.DPathCompileInfo
import org.apache.daffodil.runtime1.dsom.RuntimeSchemaDefinitionError
import org.apache.daffodil.runtime1.dsom.RuntimeSchemaDefinitionWarning
import org.apache.daffodil.runtime1.dsom.SchemaDefinitionErrorFromWarning
import org.apache.daffodil.runtime1.dsom.ValidationError
import org.apache.daffodil.runtime1.infoset.DataValue.DataValuePrimitive
import org.apache.daffodil.runtime1.infoset._
Expand Down Expand Up @@ -574,7 +575,15 @@ abstract class ParseOrUnparseState protected (
if (!suppress) {
val rsdw =
new RuntimeSchemaDefinitionWarning(warnID, ctxt.schemaFileLocation, this, str, args: _*)
diagnostics = rsdw :: diagnostics
if (tunable.escalateWarningsToErrors) {
val sde = new SchemaDefinitionErrorFromWarning(
rsdw,
warnID
)
ctxt.toss(sde)
} else {
diagnostics = rsdw :: diagnostics
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@
<xs:appinfo source="http://www.ogf.org/dfdl/dfdl-1.0/" />
</xs:annotation>
</xs:element>

<xs:element name="elem2" type="xs:string" daf:suppressSchemaDefinitionWarnings="appinfoDFDLSourceWrong">
<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/dfdl-1.0/" />
</xs:annotation>
</xs:element>
</tdml:defineSchema>


Expand All @@ -339,10 +345,31 @@
config="escalateWarnings">
<tdml:document><![CDATA[test]]></tdml:document>
<tdml:errors>
<tdml:error>Schema Definition Error</tdml:error>
<tdml:error>warnings escalated to errors</tdml:error>
<tdml:error>Schema Definition Warning Escalated Error</tdml:error>
<tdml:error>warning escalated to error</tdml:error>
<tdml:error>appinfoDFDLSourceWrong</tdml:error>
<tdml:error>xs:appinfo source attribute</tdml:error>
</tdml:errors>
</tdml:parserTestCase>

<!--
Test Name: schema_warning_escalated_to_error2
Schema: warning_escalated
Root: elem2
Purpose: This test demonstrates escalating warnings to errors doesn't happen when
warning is suppressed
-->

<tdml:parserTestCase name="schema_warning_escalated_to_error2" root="elem2"
model="warning_escalated"
ignoreUnexpectedWarnings="false"
config="escalateWarnings">
<tdml:document><![CDATA[test]]></tdml:document>
<tdml:infoset>
<tdml:dfdlInfoset>
<elem2>test</elem2>
</tdml:dfdlInfoset>
</tdml:infoset>
</tdml:parserTestCase>

</tdml:testSuite>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ct="http://w3.ibm.com/xmlns/dfdl/ctInfoset"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:ex="http://example.com"
xmlns:daf="urn:ogf:dfdl:2013:imp:daffodil.apache.org:2018:ext"
defaultRoundTrip="true">

<tdml:defineSchema name="v">
Expand Down Expand Up @@ -2221,6 +2222,27 @@
</tdml:parserTestCase>


<tdml:defineConfig name="escalateWarnings">
<daf:tunables>
<daf:escalateWarningsToErrors>true</daf:escalateWarningsToErrors>
</daf:tunables>
</tdml:defineConfig>

<tdml:parserTestCase name="defineVariable_nonConstantExpression_setVar_err2" root="root"
model="defineVariable_nonConstantExpression_setVar" description="escalate warning"
config="escalateWarnings">
<tdml:document/>
<tdml:errors>
<tdml:error>Schema Definition Warning Escalated Error</tdml:error>
<tdml:error>warning escalated to error</tdml:error>
<tdml:error>variableSet</tdml:error>
<tdml:error>Cannot set variable</tdml:error>
<tdml:error>after reading the default value</tdml:error>
<tdml:error>State was: VariableRead</tdml:error>
</tdml:errors>
</tdml:parserTestCase>


<tdml:defineSchema name="variable_direction_schema">
<xs:include schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
<dfdl:format ref="ex:GeneralFormat"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ class TestSDE {
@Test def test_schema_warning_escalated_to_error(): Unit = {
runner.runOneTest("schema_warning_escalated_to_error")
}

@Test def test_schema_warning_escalated_to_error2(): Unit = {
runner.runOneTest("schema_warning_escalated_to_error2")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class TestVariables {
@Test def test_defineVariable_nonConstantExpression_setVar_err(): Unit = {
runner.runOneTest("defineVariable_nonConstantExpression_setVar_err")
}
@Test def test_defineVariable_nonConstantExpression_setVar_err2(): Unit = {
runner.runOneTest("defineVariable_nonConstantExpression_setVar_err2")
}

// DAFFODIL-2444 - This test triggers an unhandled NoSuchElement exception, which if handled then runs into an Assert.invariant
// @Test def test_defineVariable_ref_infoset_err(): Unit = { runner.runOneTest("defineVariable_ref_infoset_err") }
Expand Down

0 comments on commit 73088d3

Please sign in to comment.