diff --git a/src/zcl_aff_test_types.clas.abap b/src/zcl_aff_test_types.clas.abap index fd125298..6dde58db 100644 --- a/src/zcl_aff_test_types.clas.abap +++ b/src/zcl_aff_test_types.clas.abap @@ -1028,6 +1028,29 @@ CLASS zcl_aff_test_types DEFINITION enum_component TYPE c LENGTH 2, END OF struc_with_own_enum_values. + CONSTANTS: + BEGIN OF co_enum_special_chars, + "!

Less Than

+ "! Less than + less_than TYPE c LENGTH 1 VALUE '<', + "!

Greater Than

+ "! Greater than + greater_than TYPE c LENGTH 1 VALUE '>', + "!

Ampersand

+ "! Ampersand + ampersand TYPE c LENGTH 1 VALUE '&', + END OF co_enum_special_chars. + + TYPES: + "!

Structure With Special Character Enum Values

+ "! Structure with special character enum values + BEGIN OF struc_with_special_char_enums, + "!

Enum With Special Characters

+ "! Enum with special characters + "! $values {@link zcl_aff_test_types.data:co_enum_special_chars } + special_char_enum TYPE c LENGTH 1, + END OF struc_with_special_char_enums. + CLASS-DATA subschema TYPE string_table. CLASS-DATA expected_var TYPE REF TO data. diff --git a/src/zcl_aff_writer_xslt.clas.abap b/src/zcl_aff_writer_xslt.clas.abap index 65718013..919eb531 100644 --- a/src/zcl_aff_writer_xslt.clas.abap +++ b/src/zcl_aff_writer_xslt.clas.abap @@ -206,7 +206,12 @@ CLASS zcl_aff_writer_xslt DEFINITION get_to_ref IMPORTING name TYPE string - RETURNING VALUE(result) TYPE string. + RETURNING VALUE(result) TYPE string, + escape_xml_chars + IMPORTING + value TYPE string + RETURNING + VALUE(result) TYPE string. ENDCLASS. @@ -459,6 +464,7 @@ CLASS zcl_aff_writer_xslt IMPLEMENTATION. METHOD get_abap_value. DATA abap_value_copy TYPE string. + DATA escaped_value TYPE string. CASE element_description->type_kind. WHEN cl_abap_typedescr=>typekind_int OR cl_abap_typedescr=>typekind_int1 OR cl_abap_typedescr=>typekind_int2 OR cl_abap_typedescr=>typekind_int8. @@ -466,9 +472,11 @@ CLASS zcl_aff_writer_xslt IMPLEMENTATION. CONDENSE abap_value_copy. result = |I({ abap_value_copy })|. WHEN cl_abap_typedescr=>typekind_num. - result = |N('{ abap_value }')|. + escaped_value = escape_xml_chars( abap_value ). + result = |N('{ escaped_value }')|. WHEN OTHERS. - result = |'{ abap_value }'|. + escaped_value = escape_xml_chars( abap_value ). + result = |'{ escaped_value }'|. ENDCASE. ENDMETHOD. @@ -811,4 +819,14 @@ CLASS zcl_aff_writer_xslt IMPLEMENTATION. ENDLOOP. ENDMETHOD. + METHOD escape_xml_chars. + result = value. + " Ampersand must be replaced first to avoid double-escaping + REPLACE ALL OCCURRENCES OF '&' IN result WITH '&'. + REPLACE ALL OCCURRENCES OF '<' IN result WITH '<'. + REPLACE ALL OCCURRENCES OF '>' IN result WITH '>'. + REPLACE ALL OCCURRENCES OF '"' IN result WITH '"'. + REPLACE ALL OCCURRENCES OF '''' IN result WITH '''. + ENDMETHOD. + ENDCLASS. diff --git a/src/zcl_aff_writer_xslt.clas.testclasses.abap b/src/zcl_aff_writer_xslt.clas.testclasses.abap index 45809c3a..c6aa4770 100644 --- a/src/zcl_aff_writer_xslt.clas.testclasses.abap +++ b/src/zcl_aff_writer_xslt.clas.testclasses.abap @@ -1297,6 +1297,7 @@ CLASS ltcl_type_writer_xslt_ad DEFINITION FINAL FOR TESTING structure_with_enums FOR TESTING RAISING cx_static_check, structure_with_default_problem FOR TESTING RAISING cx_static_check, struc_with_own_enum_values FOR TESTING RAISING cx_static_check, + struc_with_special_char_enums FOR TESTING RAISING cx_static_check, enable_extension FOR TESTING RAISING cx_static_check. ENDCLASS. @@ -2566,6 +2567,39 @@ CLASS ltcl_type_writer_xslt_ad IMPLEMENTATION. validate_output( act_output ). ENDMETHOD. + METHOD struc_with_special_char_enums. + DATA test_type TYPE zcl_aff_test_types=>struc_with_special_char_enums. + DATA(act_output) = test_generator->generate_type( test_type ). + me->exp_transformation = VALUE #( + ( `` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` <_ tt:lax="on">` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( ` <__/>` ) + ( ` ` ) + ( ` ` ) + ( ` ` ) + ( `` ) ). + validate_output( act_output ). + ENDMETHOD. + METHOD validate_output. DATA exp TYPE string_table. @@ -2653,6 +2687,8 @@ RISK LEVEL DANGEROUS. struc_with_own_enum_values FOR TESTING RAISING cx_static_check, + struc_with_special_char_enums FOR TESTING RAISING cx_static_check, + from_abap_to_json IMPORTING test_type TYPE data @@ -3224,6 +3260,40 @@ CLASS ltcl_integration_test_ad IMPLEMENTATION. act_data = test_type ). ENDMETHOD. + METHOD struc_with_special_char_enums. + DATA test_type TYPE zcl_aff_test_types=>struc_with_special_char_enums. + test_type = VALUE #( special_char_enum = '<' ). + exp_json = VALUE #( + ( `{` ) + ( ` "specialCharEnum": "lessThan"` ) + ( `}` ) ). + do_integration_test( + EXPORTING + test_type = test_type + CHANGING + act_data = test_type ). + test_type = VALUE #( special_char_enum = '>' ). + exp_json = VALUE #( + ( `{` ) + ( ` "specialCharEnum": "greaterThan"` ) + ( `}` ) ). + do_integration_test( + EXPORTING + test_type = test_type + CHANGING + act_data = test_type ). + test_type = VALUE #( special_char_enum = '&' ). + exp_json = VALUE #( + ( `{` ) + ( ` "specialCharEnum": "ampersand"` ) + ( `}` ) ). + do_integration_test( + EXPORTING + test_type = test_type + CHANGING + act_data = test_type ). + ENDMETHOD. + METHOD do_integration_test. from_abap_to_json( EXPORTING