This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 8.0.x-hibernate7-dev in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 49bf2367b585d047f5b4f8711ab35a970437932a Author: Walter Duque de Estrada <[email protected]> AuthorDate: Tue Mar 10 08:51:13 2026 -0500 Refactor ChangelogXml2Groovy for @CompileStatic and add tests - Add missing Node import and fix static compilation warnings. - Initialize mixedText to null to ensure it is always assigned before use. - Add ChangelogXml2GroovySpec to verify XML to Groovy DSL conversion. - Fix escaping behavior for special characters in generated DSL. --- .../liquibase/ChangelogXml2Groovy.groovy | 15 +-- .../liquibase/ChangelogXml2GroovySpec.groovy | 101 +++++++++++++++++++++ 2 files changed, 110 insertions(+), 6 deletions(-) diff --git a/grails-data-hibernate7/dbmigration/src/main/groovy/org/grails/plugins/databasemigration/liquibase/ChangelogXml2Groovy.groovy b/grails-data-hibernate7/dbmigration/src/main/groovy/org/grails/plugins/databasemigration/liquibase/ChangelogXml2Groovy.groovy index 3e92e62d03..99acf42651 100644 --- a/grails-data-hibernate7/dbmigration/src/main/groovy/org/grails/plugins/databasemigration/liquibase/ChangelogXml2Groovy.groovy +++ b/grails-data-hibernate7/dbmigration/src/main/groovy/org/grails/plugins/databasemigration/liquibase/ChangelogXml2Groovy.groovy @@ -41,8 +41,11 @@ class ChangelogXml2Groovy { def groovy = new StringBuilder('databaseChangeLog = {') groovy.append(NEWLINE) - new XmlParser(false, false).parseText(xml).each { Node node -> - convertNode(node, groovy, 1) + Node root = new XmlParser(false, false).parseText(xml) + root.children().each { Object child -> + if (child instanceof Node) { + convertNode(child, groovy, 1) + } } groovy.append('}') groovy.append(NEWLINE) @@ -54,7 +57,7 @@ class ChangelogXml2Groovy { groovy.append(NEWLINE) appendWithIndent(indentLevel, groovy, (String) node.name()) - String mixedText + String mixedText = null def children = [] for (child in node.children()) { if (child instanceof String) { @@ -90,10 +93,10 @@ class ChangelogXml2Groovy { delimiter = ', ' } - node.attributes().each { name, value -> + node.attributes().each { Object name, Object value -> local.append(delimiter) - local.append(name) - local.append(': "').append(((String) value).replaceAll(/(\$|\\|\\n)/, /\\$1/)).append('"') + local.append(name.toString()) + local.append(': "').append(value.toString().replaceAll(/(\$|\\|\\n)/, /\\$1/)).append('"') delimiter = ', ' } diff --git a/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/liquibase/ChangelogXml2GroovySpec.groovy b/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/liquibase/ChangelogXml2GroovySpec.groovy new file mode 100644 index 0000000000..eb082d3838 --- /dev/null +++ b/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/liquibase/ChangelogXml2GroovySpec.groovy @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.plugins.databasemigration.liquibase + +import spock.lang.Specification +import spock.lang.Unroll + +class ChangelogXml2GroovySpec extends Specification { + + @Unroll + void "test convert simple xml to groovy"() { + given: + def xml = """ +<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> + <changeSet author="burt" id="1"> + <createTable tableName="test"> + <column name="id" type="int"> + <constraints primaryKey="true" nullable="false"/> + </column> + </createTable> + </changeSet> +</databaseChangeLog> +""" + when: + String groovy = ChangelogXml2Groovy.convert(xml) + + then: + groovy.contains('databaseChangeLog = {') + groovy.contains('changeSet(author: "burt", id: "1") {') + groovy.contains('createTable(tableName: "test") {') + groovy.contains('column(name: "id", type: "int") {') + groovy.contains('constraints(primaryKey: "true", nullable: "false")') + } + + void "test convert with attributes and nesting"() { + given: + def xml = """ +<databaseChangeLog> + <property name="foo" value="bar"/> + <changeSet author="beckwith" id="2"> + <addColumn tableName="test"> + <column name="new_col" type="varchar(255)"/> + </addColumn> + </changeSet> +</databaseChangeLog> +""" + when: + String groovy = ChangelogXml2Groovy.convert(xml) + + then: + groovy.contains('property(name: "foo", value: "bar")') + groovy.contains('changeSet(author: "beckwith", id: "2") {') + groovy.contains('addColumn(tableName: "test") {') + groovy.contains('column(name: "new_col", type: "varchar(255)")') + } + + void "test escaping special characters"() { + given: + def xml = """ +<databaseChangeLog> + <changeSet author="test" id="3"> + <sql>select * from foo where name = '\$name' and path = 'C:\\\\temp'</sql> + </changeSet> +</databaseChangeLog> +""" + when: + String groovy = ChangelogXml2Groovy.convert(xml) + + then: + groovy.contains('sql("""select * from foo where name = \'\\$name\' and path = \'C:\\\\\\\\temp\'""")') + } + + void "test empty changelog"() { + given: + def xml = "<databaseChangeLog></databaseChangeLog>" + + when: + String groovy = ChangelogXml2Groovy.convert(xml) + + then: + groovy.trim() == "databaseChangeLog = {\n}" + } +}
