oraclerestpostsys-refcursororacle-ords

Issue with Oracle REST POST request returning sys_refcursor


We are currently prototyping a REST API using the Oracle APEX cloud free tier. I’m having an issue with returning a sys_refcursor in an Oracle REST POST request. Here’s the particulars (I'm new at asking questions here so I hope the formatting is readable:

TABLE

 CREATE TABLE "WF_PROTOTYPE"."INGREDIENT_TYPES"     
(   "ID" NUMBER(10,0)  NOT NULL ENABLE,     
    "ACCOUNT_ID" NUMBER(10,0) NOT NULL ENABLE,
    "NAME"  VARCHAR2(100 CHAR) COLLATE "USING_NLS_COMP" NOT NULL ENABLE, 
    "CREATED_AT" TIMESTAMP (6),     "UPDATED_AT" TIMESTAMP (6), 
    "DELETED_AT" TIMESTAMP (6),     "CREATED_BY" NUMBER(10,0), 
    "UPDATED_BY" NUMBER(10,0),      "DELETED_BY" NUMBER(10,0), 
    "DESCRIPTION" VARCHAR2(1000 BYTE) COLLATE "USING_NLS_COMP",     
 CONSTRAINT "INGR_TYPE_PK" PRIMARY KEY ("ID"))

TRIGGER

create or replace TRIGGER ingredient_types_id_TRIG BEFORE INSERT OR UPDATE ON ingredient_types
FOR EACH ROW
DECLARE 
v_newVal NUMBER(12) := 0;
v_incval NUMBER(12) := 0;
BEGIN
  IF INSERTING AND :new.id IS NULL THEN
    SELECT  ingredient_types_id_SEQ.NEXTVAL INTO v_newVal FROM DUAL;
    -- If this is the first time this table have been inserted into (sequence == 1)
    IF v_newVal = 1 THEN 
      --get the max indentity value from the table
      SELECT NVL(max(id),0) INTO v_newVal FROM ingredient_types;
      v_newVal := v_newVal + 1;
      --set the sequence to that value
      LOOP
           EXIT WHEN v_incval>=v_newVal;
           SELECT ingredient_types_id_SEQ.nextval INTO v_incval FROM dual;
      END LOOP;
    END IF;
   -- assign the value from the sequence to emulate the identity column
   :new.id := v_newVal;
  END IF;
END;

STORED PROCEDURE

create or replace PROCEDURE ingredient_type_POST (
   p_account_id     IN  ingredient_types.account_id%TYPE,
   p_name           IN  ingredient_types.name%TYPE,
   p_description    in  ingredient_types.description%type,
   p_created_by     IN  ingredient_types.created_by%type,
   p_out_rec        OUT sys_refcursor
)
AS
    new_id ingredient_types.id%type;
BEGIN
   INSERT INTO ingredient_types (account_id, name, DESCRIPTION, created_at, created_by)
   VALUES (p_account_id, p_name, p_description, systimestamp, nvl(p_created_by,2))
   RETURN id INTO new_id;

   OPEN p_out_rec FOR
    SELECT id, account_id, name, description
    FROM   ingredient_types
    WHERE  ID = new_id;

EXCEPTION
   WHEN OTHERS
   THEN HTP.print(SQLERRM);
END ingredient_type_POST;

ORDS DEFINITION

ORDS.DEFINE_HANDLER(
      p_module_name    => 'wf_api.rest',
      p_pattern        => 'accounts/:acct_id/ingredient_types',
      p_method         => 'POST',
      p_source_type    => 'plsql/block',
      p_items_per_page =>  0,
      p_mimes_allowed  => '',
      p_comments       => 'CREATE an Ingredient Type',
      p_source         => 
'begin
    INGREDIENT_TYPE_POST(
                p_account_id    => :acct_id, 
                p_name          => :name, 
                p_description   => :description,
                p_created_by    => :APP_USER, 
                p_rec           => :new_rec);
end;'
      );
  ORDS.DEFINE_PARAMETER(
      p_module_name        => 'wf_api.rest',
      p_pattern            => 'accounts/:acct_id/ingredient_types',
      p_method             => 'POST',
      p_name               => 'rec',
      p_bind_variable_name => 'new_rec',
      p_source_type        => 'RESPONSE',
      p_param_type         => 'RESULTSET',
      p_access_method      => 'OUT',
      p_comments           => NULL);      

When I run the SP from the SQL Developer worksheet EDITOR, filling in the input variables I get a successful result (New record created) and it returns the anticipated resultset in the p_out_rec parameter. However, when I run it in the HANDLER editor, filling out the appropriate variables, I get the following error message:

Error starting at line : 1 in command -
begin
    INGREDIENT_TYPE_POST(
                p_account_id    => :acct_id, 
                p_name          => :name, 
                p_description   => :description,
                p_created_by    => :APP_USER, 
                p_rec           => :new_rec);
end;
Error report -
ORA-06550: line 2, column 5:
PLS-00306: wrong number or types of arguments in call to 'INGREDIENT_TYPE_POST'
ORA-06550: line 2, column 5:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

I’ve tried to mimic ThatJeffSmith’s tutorial https://www.thatjeffsmith.com/archive/2017/03/parameters-and-binds-for-your-restful-services-resultsets/ as closely as possible, and I’m not seeing where my fault lies… any insights greatly appreciated!!!


Solution

  • Found a couple of issues with your code.

    1. you're not supplying a value for the table's ID column
    2. your refcursor is being populated with a query that will mostly never work, you're checking the ID against the ACCOUNT_ID value
    3. you're referring to p_out_rec as p_rec -- thanks @andrew!

    Here's how it could work

    enter image description here

    -- Generated by ORDS REST Data Services 20.4.1.r0131644
    -- Schema: HR  Date: Wed Mar 17 05:24:17 2021 
    --
    
    BEGIN
      ORDS.ENABLE_SCHEMA(
          p_enabled             => TRUE,
          p_schema              => 'HR',
          p_url_mapping_type    => 'BASE_PATH',
          p_url_mapping_pattern => 'hr',
          p_auto_rest_auth      => FALSE);
        
      ORDS.DEFINE_MODULE(
          p_module_name    => 'wf_api.rest',
          p_base_path      => '/wf_api/',
          p_items_per_page => 25,
          p_status         => 'PUBLISHED',
          p_comments       => 'stackoverflow question');
    
      ORDS.DEFINE_TEMPLATE(
          p_module_name    => 'wf_api.rest',
          p_pattern        => 'accounts/:acct_id/ingredient_types',
          p_priority       => 0,
          p_etag_type      => 'HASH',
          p_etag_query     => NULL,
          p_comments       => NULL);
    
      ORDS.DEFINE_HANDLER(
          p_module_name    => 'wf_api.rest',
          p_pattern        => 'accounts/:acct_id/ingredient_types',
          p_method         => 'POST',
          p_source_type    => 'plsql/block',
          p_mimes_allowed  => '',
          p_comments       => NULL,
          p_source         => 
    'begin
     INGREDIENT_TYPE_POST(
        P_ID             => :id,
        P_ACCOUNT_ID     => :acct_id,
        P_NAME           => :name,
        P_DESCRIPTION    => :description,
        P_CREATED_BY     => :APP_USER,
        P_OUT_REC        => :new_rec
      );
    END;');
    
      ORDS.DEFINE_PARAMETER(
          p_module_name        => 'wf_api.rest',
          p_pattern            => 'accounts/:acct_id/ingredient_types',
          p_method             => 'POST',
          p_name               => 'rec',
          p_bind_variable_name => 'new_rec',
          p_source_type        => 'RESPONSE',
          p_param_type         => 'RESULTSET',
          p_access_method      => 'OUT',
          p_comments           => NULL);
    
        
            
    COMMIT;
    
    END;
    

    And the PL/SQL proc...

    create or replace PROCEDURE ingredient_type_POST (
       p_id             IN  ingredient_types.id%TYPE,
       p_account_id     IN  ingredient_types.account_id%TYPE,
       p_name           IN  ingredient_types.name%TYPE,
       p_description    in  ingredient_types.description%type,
       p_created_by     IN  ingredient_types.created_by%type,
       p_out_rec        OUT sys_refcursor
    )
    AS
        new_id ingredient_types.id%type;
    BEGIN
       INSERT INTO ingredient_types (id, account_id, name, DESCRIPTION, created_at, created_by)
       VALUES (p_id, p_account_id, p_name, p_description, systimestamp, nvl(p_created_by,2))
       RETURN id INTO new_id;
    
       OPEN p_out_rec FOR
        SELECT id, account_id, name, description
        FROM   ingredient_types
        WHERE  account_ID = p_account_id;
    
    EXCEPTION
       WHEN OTHERS
       THEN HTP.print(SQLERRM);
    END ingredient_type_POST;
    

    And the request -

    curl --request POST \
      --url http://localhost:8080/ords/hr/wf_api/accounts/42/ingredient_types \
      --header 'Content-Type: application/json' \
      --data '{
        "id".         : 55,
        "name"        : "heyYou",
        "description" : "some words",
        "APP_USER"    : 44
    }'