Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
Allow dots in XSOData property name (#1378)
Browse files Browse the repository at this point in the history
* Allow dots in XSOData property name
  • Loading branch information
MarinHadzhiev authored Mar 9, 2022
1 parent 3d271f6 commit 5906a67
Show file tree
Hide file tree
Showing 9 changed files with 653 additions and 542 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

public class XSKOData2ODataMTransformer {

private OData2ODataMTransformer oData2ODataMTransformer = new OData2ODataMTransformer();
private OData2ODataMTransformer oData2ODataMTransformer = new OData2ODataMTransformer(new XSODataPropertyNameEscaper());

public String[] transform(ODataDefinition oDataDefinition) throws SQLException {
try {
return oData2ODataMTransformer.transform(oDataDefinition);
} catch (Exception e) {
XSKCommonsUtils.logProcessorErrors(e.getMessage(), XSKCommonsConstants.PROCESSOR_ERROR, oDataDefinition.getLocation(), XSKCommonsConstants.XSK_ODATA_PARSER);
throw e;
public String[] transform(ODataDefinition oDataDefinition) throws SQLException {
try {
return oData2ODataMTransformer.transform(oDataDefinition);
} catch (Exception e) {
XSKCommonsUtils.logProcessorErrors(e.getMessage(), XSKCommonsConstants.PROCESSOR_ERROR, oDataDefinition.getLocation(), XSKCommonsConstants.XSK_ODATA_PARSER);
throw e;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

public class XSKOData2ODataXTransformer {

private OData2ODataXTransformer oData2ODataXTransformer = new OData2ODataXTransformer(new XSKTableMetadataProvider());
private OData2ODataXTransformer oData2ODataXTransformer = new OData2ODataXTransformer(new XSKTableMetadataProvider(), new XSODataPropertyNameEscaper());

public String[] transform(ODataDefinition oDataDefinition) throws SQLException {
try {
return oData2ODataXTransformer.transform(oDataDefinition);
} catch (Exception e) {
XSKCommonsUtils.logProcessorErrors(e.getMessage(), XSKCommonsConstants.PROCESSOR_ERROR, oDataDefinition.getLocation(), XSKCommonsConstants.XSK_ODATA_PARSER);
throw e;
public String[] transform(ODataDefinition oDataDefinition) throws SQLException {
try {
return oData2ODataXTransformer.transform(oDataDefinition);
} catch (Exception e) {
XSKCommonsUtils.logProcessorErrors(e.getMessage(), XSKCommonsConstants.PROCESSOR_ERROR, oDataDefinition.getLocation(), XSKCommonsConstants.XSK_ODATA_PARSER);
throw e;
}
}
}
}
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;
}
}
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);
}

}
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));
}

}
Loading

0 comments on commit 5906a67

Please sign in to comment.