Skip to content

Commit

Permalink
Fixed RD-5910: XML typechecker shouldn't accept complex types for att…
Browse files Browse the repository at this point in the history
…ributes (#152)
  • Loading branch information
bgaidioz authored Aug 30, 2023
1 parent 86e93d6 commit 4b52200
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,22 @@ trait XmlEntryExtensionHelper extends EntryExtensionHelper {
r
}

protected def validateAttributeType(t: Type): Either[Seq[UnsupportedType], Type] = t match {
case _: Rql2PrimitiveType => Right(t)
case Rql2ListType(primitive: Rql2PrimitiveType, _) => Right(Rql2ListType(primitive)) // strip the tryable/nullable
case Rql2IterableType(primitive: Rql2PrimitiveType, _) =>
Right(Rql2IterableType(primitive)) // strip the tryable/nullable
case _ => Left(Seq(UnsupportedType(t, t, None)))
}

protected def validateXmlType(t: Type): Either[Seq[UnsupportedType], Type] = t match {
case _: Rql2LocationType | _: Rql2RegexType => Left(Seq(UnsupportedType(t, t, None)))
case t: Rql2RecordType =>
val atts = t.atts
.map(x => x.idn -> validateXmlType(x.tipe))
.map { x =>
val validation = if (x.idn.startsWith("@")) validateAttributeType(x.tipe) else validateXmlType(x.tipe)
x.idn -> validation
}
val errors = atts.collect { case (_, Left(error)) => error }
if (errors.nonEmpty) Left(errors.flatten)
else Right(Rql2RecordType(atts.map(x => Rql2AttrType(x._1, x._2.right.get)), t.props))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,4 +642,28 @@ trait XmlPackageTest extends CompilerTestContext {
| Xml.Parse(s, type record(tag: list(byte), notag: list(byte), `#text`: list(byte), `@att`: list(byte)))
|""".stripMargin
)(it => it should evaluateTo("{tag: [Byte.From(2)], notag: [], `#text`: [Byte.From(3)], `@att`: [Byte.From(1)]}"))

// stripping unwanted fields.
test(snapi"""Xml.Read("$data", type record(`@place`: string, name: string))""") { it =>
it should typeAs("record(`@place`: string, name: string)")
it should evaluateTo("{`@place`: \"world\", name: \"john\"}")
}

// lists are accepted when parsing an attribute.
test(snapi"""Xml.Read("$data", type record(`@place`: list(string), name: string))""") { it =>
it should typeAs("record(`@place`: list(string), name: string)")
it should evaluateTo("{`@place`: [\"world\"], name: \"john\"}")
}

// collections are accepted when parsing an attribute.
test(snapi"""Xml.Read("$data", type record(`@place`: collection(string), name: string))""") { it =>
it should typeAs("record(`@place`: collection(string), name: string)")
it should evaluateTo("{`@place`: [\"world\"], name: \"john\"}")
}

// other types than primitives, lists and collections are not accepted when parsing an attribute (RD-5910).
test(snapi"""Xml.Read("$data", type record(`@place`: record(a: string), name: string))""") { it =>
it should typeErrorAs("unsupported type")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public class JdbcParserRawTruffleException extends RawTruffleRuntimeException {

public JdbcParserRawTruffleException(String message, Throwable e, Node location) {
super(String.format("failed to read value: %s", message), e, location);
}
public JdbcParserRawTruffleException(String message, Throwable e, Node location) {
super(String.format("failed to read value: %s", message), e, location);
}
}

0 comments on commit 4b52200

Please sign in to comment.