Skip to Content

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

To report this post you need to login first.

9 Comments

You must be Logged on to comment or reply to a post.

  1. Chairat Onyaem

    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.

    (0) 
    1. Lars Hvam
      Post author

      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.

      (1) 
      1. Andre Fischer

        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.

         

         

        (2) 
      2. Chairat Onyaem

        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.

         

        (0) 
  2. Bilen Cekic

    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.

    (0) 
      1. Bilen Cekic

        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 🙂

        (2) 

Leave a Reply