Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
larshp
Active Contributor

Swagger


Swagger is a set of tools for creating, generating, documenting, and testing RESTful services.
It is centered around a specification file(https://swagger.io/specification/), which contains the description of the REST services, much like a SOAP WSDL or OData metadata file.

There are many powerful tools in the Swagger family, which all are open source:

These tools are free and works in the browser. SAP also use Swagger as part of API management.

 

ABAP


A while ago I started building a tool for integrating ABAP and Swagger using an inside-out approach.

Lets jump right into it and see how it works, suppose I want to expose the methods of the following class as REST services:
CLASS zcl_todo DEFINITION PUBLIC CREATE PUBLIC.

PUBLIC SECTION.
METHODS create
IMPORTING
is_data TYPE ztodo_data
RETURNING
VALUE(rs_key) TYPE ztodo_key .
METHODS delete
IMPORTING
is_key TYPE ztodo_key .
METHODS list
RETURNING
VALUE(rt_list) TYPE ztodo_tt .
METHODS update
IMPORTING
iv_guid TYPE ztodo_key-guid
is_data TYPE ztodo_data .

ENDCLASS.

CLASS ZCL_TODO IMPLEMENTATION.

METHOD create.

DATA: ls_todo TYPE ztodo.


TRY.
rs_key-guid = cl_system_uuid=>if_system_uuid_static~create_uuid_c22( ).
CATCH cx_uuid_error.
ASSERT 0 = 1.
ENDTRY.

MOVE-CORRESPONDING rs_key TO ls_todo.
MOVE-CORRESPONDING is_data TO ls_todo.

INSERT ztodo FROM ls_todo.
ASSERT sy-subrc = 0.

ENDMETHOD.

METHOD delete.

DELETE FROM ztodo WHERE guid = is_key-guid.
ASSERT sy-subrc = 0.

ENDMETHOD.

METHOD list.

SELECT * FROM ztodo INTO TABLE rt_list. "#EC CI_NOWHERE

ENDMETHOD.

METHOD update.

DATA: ls_todo TYPE ztodo.


ls_todo-guid = iv_guid.
MOVE-CORRESPONDING is_data TO ls_todo.

UPDATE ztodo FROM ls_todo.
ASSERT sy-subrc = 0.

ENDMETHOD.
ENDCLASS.

Interface ZIF_SWAG_HANDLER must be implemented, and metadata added for the methods to be exposed,
  METHOD zif_swag_handler~meta.

FIELD-SYMBOLS: <ls_meta> LIKE LINE OF rt_meta.


APPEND INITIAL LINE TO rt_meta ASSIGNING <ls_meta>.
<ls_meta>-summary = 'List'(001).
<ls_meta>-url-regex = '/list$'.
<ls_meta>-method = zcl_swag=>c_method-get.
<ls_meta>-handler = 'LIST'.

APPEND INITIAL LINE TO rt_meta ASSIGNING <ls_meta>.
<ls_meta>-summary = 'Create'(002).
<ls_meta>-url-regex = '/create$'.
<ls_meta>-method = zcl_swag=>c_method-post.
<ls_meta>-handler = 'CREATE'.

APPEND INITIAL LINE TO rt_meta ASSIGNING <ls_meta>.
<ls_meta>-summary = 'Delete'(003).
<ls_meta>-url-regex = '/delete$'.
<ls_meta>-method = zcl_swag=>c_method-post.
<ls_meta>-handler = 'DELETE'.

APPEND INITIAL LINE TO rt_meta ASSIGNING <ls_meta>.
<ls_meta>-summary = 'Update'(004).
<ls_meta>-url-regex = '/update/(w+)$'.
APPEND 'IV_GUID' TO <ls_meta>-url-group_names.
<ls_meta>-method = zcl_swag=>c_method-post.
<ls_meta>-handler = 'UPDATE'.

ENDMETHOD.

Add the handler to a custom SICF node, and thats about it! ABAP-Swagger will automatically generate a spec from the method definitions, and it is possible to use the Swagger UI to test the services. ABAP-Swagger is open source and works from 702 and up.



The full example can be found at https://github.com/larshp/todo_logic

 

Find more Open Source ABAP projects on dotabap.org

 

ABAP is like the sun, it will keep shining for a million years
16 Comments
chairat_onyaem
Participant
0 Kudos
Well, I wish I had known this sooner...  Developing REST Handler + REST Resource-classes every time would be kind of painful.

BTW, do you have any suggestion when we should use or not use OData? Now, I keep telling people to go for OData as the 1st option until they hit some limitations or until OData made things too complicated for them.
larshp
Active Contributor
Use OData as it is recommended by SAP(link?).

However, sometimes it might be easier just to expose a plain REST, if the service is small, if there is a lot of governance around OData services in the organization. Also note that here is a lot of things you get for free when choosing Gateway/Odata like authorizations and error handling/tracing.

For Open Source tools it is difficult to use OData, as it requires the user to have Gateway in their landscape, with above solution there is only a requirement to have 702 or higher.
Andre_Fischer
Product and Topic Expert
Product and Topic Expert
Just my 2 cents with regards to the required SAP NW releases for both approaches.

SAP Gateway can be made available on any SAP Business Suite System that is running on top of SAP NetWeaver 7.0, 7.01 and 7.02 by installing the AddOns IW_BEP, IW_FND and GW_CORE as described in the SAP Gateway installation guide. So you can make SAP Gateway available already on SAP NetWeaver ABAP systems prior to 7.02.

As of SAP NW 740 the software component SAP_GWFND is part of the SAP Basis and SAP Gateway can thus be activated on any recent SAP NetWeaver AS ABAP server without the need to install any AddOn.

 

 
RalfHandl
Product and Topic Expert
Product and Topic Expert

Swagger and OData are not mutually exclusive: you can easily create Swagger descriptions from OData $metadata, see https://github.com/oasis-tcs/odata-openapi

You can see this in action in the SAP API Business Hub.

larshp
Active Contributor
0 Kudos
Cool 🙂
chairat_onyaem
Participant
0 Kudos
Another complaint I heard from Java developer who consuming OData is that OData makes their life more complicated. They need either to learn or use an extra library to handle it.

However, OData seems to have better data abstraction and governance. No questions on API design.

 
former_member185511
Active Participant
0 Kudos
cool! that is how i use oData. I create 1 service from SEGW with a structure which has 5 columns only. One column which is CONTEXT always send and gets JSON data from ABAP. So just with one service i can complete whole application. (GET and POST methods simply but JSON data is moved  in column named CONTEXT). I use oData to handle errros, tracing, security and auth but i supported it in the backend with a strategy design pattern approach an decide what class to trigger when the request comes.
chairat_onyaem
Participant
0 Kudos
Sounds interesting . Any chance to share some code snippets?

 
former_member185511
Active Participant
yes my project already went live 🙂 i am writing blog post soon. I used above approach for my last 4 project and result is very good. i will share all 🙂
former_member197696
Participant
0 Kudos
HI Lars,

Thanks for the blog post. I have given it a try and followed all your steps.

I have added the handler(ZCL_TODO_SICF)  to a  SICF node and also added Swagger to Localhost.

Can you please guide me the next steps if possible.

I mean how to configure swagger as I really want to check the output of how swagger works with sap.

 

Thanks in advance

SSK

 

 
larshp
Active Contributor
0 Kudos
Hi, please open an issue on github, then I'll try to help you out 🙂
0 Kudos
Hi All,

Kindly help with an example as how to pass value for a parameter in the get api call in URL.

I have implemented the ABAP code available in GIT and enabled the service via SICF. The call flows through backend and the respective handler method gets called based on the meta method being implemented.

I am stuck with passing parameter value so that it gets populated in the parameters structure.

 

Regards, Sharath
larshp
Active Contributor
0 Kudos

see https://github.com/larshp/abapGitServer/blob/master/src/service/zcl_ags_service_rest.clas.abap for some examples

also feel free to open an issue on github

larshp
Active Contributor
pbist0103
Participant

lars.hvam SAP should hire you. Man you think more like the us, the mortal developers and try to solve the challenges we face.

At least SAP ABAP tooling team should have you on-board to be in touch with reality.

 

larshp
Active Contributor
0 Kudos
ðŸ¤