Skip to content

Commit

Permalink
Merge pull request #26931 from mshima/parse-pom
Browse files Browse the repository at this point in the history
keep xml tag value as string when parsing pom.xml
  • Loading branch information
DanielFran authored Aug 9, 2024
2 parents 43a8b45 + 54af4f0 commit 6b5829b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
3 changes: 2 additions & 1 deletion generators/maven/internal/xml-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ const defaultXmlCommonOptions: Partial<X2jOptions & XmlBuilderOptions> = {
ignoreAttributes: false,
attributeNamePrefix: '@@',
commentPropName: '#comment',
parseAttributeValue: false,
};

export const defaultXmlParserOptions: Partial<X2jOptions> = {
parseAttributeValue: false,
parseTagValue: false,
...defaultXmlCommonOptions,
};

Expand Down
49 changes: 49 additions & 0 deletions generators/maven/support/dependabot-maven.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed 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.
*/
import { describe, expect } from 'esmocha';
import { parseMavenPom } from './dependabot-maven.js';

describe('parseMavenPom', () => {
it('should parse a pom file', () => {
const pomContent = `<?xml version="1.0" encoding="UTF-8"?>
<project>
<artifactId>test</artifactId>
<version>1.0</version>
<properties>
<test.version>1.0</test.version>
</properties>
</project>`;
const pom = parseMavenPom(pomContent);
expect(pom).toMatchInlineSnapshot(`
{
"?xml": {
"@@encoding": "UTF-8",
"@@version": "1.0",
},
"project": {
"artifactId": "test",
"properties": {
"test.version": "1.0",
},
"version": "1.0",
},
}
`);
});
});
5 changes: 3 additions & 2 deletions generators/maven/support/dependabot-maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
* limitations under the License.
*/
import { XMLParser } from 'fast-xml-parser';
import { defaultXmlParserOptions } from '../internal/xml-store.js';

/**
* Extract properties from pom content
* @param pomContent
*/
export function getPomProperties(pomContent: string): Record<string, string> {
return new XMLParser().parse(pomContent).project.properties;
return new XMLParser(defaultXmlParserOptions).parse(pomContent).project.properties;
}

export type MavenPom = {
Expand All @@ -44,7 +45,7 @@ export type MavenPomAndVersions = {
* @param fileContent
*/
export function parseMavenPom(fileContent: string): MavenPom {
return new XMLParser().parse(fileContent);
return new XMLParser(defaultXmlParserOptions).parse(fileContent);
}

/**
Expand Down

0 comments on commit 6b5829b

Please sign in to comment.