This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow dots in XSOData property name (#1378)
* Allow dots in XSOData property name
- Loading branch information
1 parent
3d271f6
commit 5906a67
Showing
9 changed files
with
653 additions
and
542 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...gine-xsodata/src/main/java/com/sap/xsk/xsodata/ds/service/XSODataPropertyNameEscaper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2022 SAP SE or an SAP affiliate company and XSK contributors | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Apache License, v2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and XSK contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package com.sap.xsk.xsodata.ds.service; | ||
|
||
import org.eclipse.dirigible.engine.odata2.transformers.ODataPropertyNameEscaper; | ||
|
||
class XSODataPropertyNameEscaper implements ODataPropertyNameEscaper { | ||
|
||
@Override | ||
public String escape(String propertyName) { | ||
return propertyName; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...engines/engine-xsodata/src/main/java/com/sap/xsk/xsodata/listener/XSODataInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2022 SAP SE or an SAP affiliate company and XSK contributors | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Apache License, v2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and XSK contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package com.sap.xsk.xsodata.listener; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.regex.Pattern; | ||
import javax.servlet.ServletContextEvent; | ||
import javax.servlet.ServletContextListener; | ||
import org.apache.olingo.odata2.core.edm.provider.EdmNamedImplProv; | ||
|
||
public class XSODataInitializer implements ServletContextListener { | ||
|
||
// OData property name pattern, Olingo default pattern do not allow dot in the property name. | ||
// Which is the case when you have a data types in HDBDD file, to support XS Classic use cases we need to allow dots in OData property names. | ||
private static final Pattern ODATA_PROPERTY_NAME_PATTERN = Pattern.compile("\\A[_\\p{L}\\p{Nl}][_.\\p{L}\\p{Nl}\\p{Nd}\\p{Mn}\\p{Mc}\\p{Pc}\\p{Cf}]{0,}\\Z"); | ||
|
||
private static final String NAME_OF_THE_PROPERTY_NAME_PATTERN_FIELD = "PATTERN_VALID_NAME"; | ||
|
||
public void contextInitialized(ServletContextEvent sce) { | ||
try { | ||
setFinalStatic(EdmNamedImplProv.class.getDeclaredField(NAME_OF_THE_PROPERTY_NAME_PATTERN_FIELD), ODATA_PROPERTY_NAME_PATTERN); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Failed to replace default Olingo OData parameter name pattern.", e); | ||
} | ||
} | ||
|
||
private void setFinalStatic(Field field, Object newValue) throws Exception { | ||
field.setAccessible(true); | ||
|
||
Field modifiersField = Field.class.getDeclaredField("modifiers"); | ||
modifiersField.setAccessible(true); | ||
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); | ||
|
||
field.set(null, newValue); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...-xsodata/src/test/java/com/sap/xsk/xsodata/ds/service/XSODataPropertyNameEscaperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.sap.xsk.xsodata.ds.service; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class XSODataPropertyNameEscaperTest { | ||
|
||
private XSODataPropertyNameEscaper escaper; | ||
|
||
@Before | ||
public void setUp() { | ||
this.escaper = new XSODataPropertyNameEscaper(); | ||
} | ||
|
||
@Test | ||
public void testEscapeWithDots() { | ||
String propertyName = "Property.Name"; | ||
assertEquals("Unexpected property name", propertyName, escaper.escape(propertyName)); | ||
} | ||
|
||
@Test | ||
public void testEscapeWithUnderscore() { | ||
String propertyName = "Property_Name"; | ||
assertEquals("Unexpected property name", propertyName, escaper.escape(propertyName)); | ||
} | ||
|
||
} |
Oops, something went wrong.