Happy Business Starts Here

Tutor

Working with a query in .Net with REST API

Does anyone have an example of issuing a query using the REST API in .Net?  I'm using the Swagger-generated library from here: https://github.com/zuora/codelibrary-dotnet

 

I can successfully call ProxyActionPOSTquery, which returns the records I request, but it can't seem to deserialize the results properly.  It tries to deserialize the results into a ProxyActionqueryResponse type, and it just ends up being null.

Tags (2)
11 REPLIES 11
Support SME

Re: Working with a query in .Net with REST API

Hello,

 

Please check out the following article for calling web API from .NET, which also includes information on deserializing. https://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

 

Make sure you're utilizing Zuora's endpoints: https://knowledgecenter.zuora.com/CA_Commerce/C_Zuora_Quotes/G_Zuora_Quotes_Configuration_Settings/A...

 






If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √

Tutor

Re: Working with a query in .Net with REST API

The API itself is not the problem.  It's returning the records I request. The problem is with the code library I mentioned.  If I use it to request a specific type of record ("AccountsApi.GETAccount", or "PaymentMethodsApi.GETPaymentMethods", for example), those work fine because they return a distinct, specifc type that the json can be mapped to ("GETAccountType", and "GETPaymentMethodType", for example).  It's the ad-hoc queries that are a problem, because they don't know what the return type is.  "ActionsApi.ProxyActionPOSTquery" returns a "ProxyActionqueryResponse" type, which is basically just a wrapper around a "ZObject", which doesn't have any properties, and so the records that come back aren't able to be deserialized into anything useful.  That's why I was looking for an example specifically relating to the action query function of the API.  I'm about to tear the code apart and try to get it to work, but wanted to see if I was doing something wrong before I reinvent the wheel.

 

By the way, the SOAP API action query works fine.  The SOAP result includes the type of object that was requested, and the SOAP "zObject" inherits from "object", so it's able to actually store a useful result.  The REST "ZObject" appears to be useless...?

Zuora Alumni

Re: Working with a query in .Net with REST API

Hi Jerrade,

 

Can you send us a isolated test class where you can reproduce this issue using the swagger generated lib?

 

We'll take a look and follow up with you.

 

Thanks,



If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √

Tutor

Re: Working with a query in .Net with REST API

Here is a simple project that shows the problem: https://github.com/jerrade/ZuoraSwaggerTest

Zuora Alumni

Re: Working with a query in .Net with REST API

Thanks for that,

 

I'm going to create a ticket for you so we can track this internally as we will probably want to engage some internal resources

 

thanks!



If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √

Zuora Documentation

Re: Working with a query in .Net with REST API

We’ve recently been looking into this issue again. I’m replying in this topic because it still gets viewed often.

 

The ZObject class should extend Dictionary<String, Object>, but swagger-codegen isn’t picking this up from our Swagger spec.

 

As a workaround, you can modify a local copy of the Swagger spec before using swagger-codegen to generate the C# library:

 

  1. Download the Swagger spec. To do this, visit https://www.zuora.com/developer/api-reference/, then click the Download button at the top of the page.

  2. Open the downloaded file in a text editor. The file is quite large, so this may take a few seconds.

  3. Locate this part of the file:

      zObject:
        additionalProperties:
          description: 'Field of the object.
    
            '
        type: object
    


    In the latest Swagger spec, this part is very close to the end of the file.

  4. Add type: object within additionalProperties, like this:

      zObject:
        additionalProperties:
          type: object
          description: 'Field of the object.
    
            '
        type: object
  5. In addition to zObject, there are some other models in the Swagger spec that contain additionalProperties without type specified. I’d recommend that you modify these models in the same way.

    All the other affected models are called <name>ObjectCustomFields. E.g., AccountObjectCustomFields, which you should modify to look like this:

      AccountObjectCustomFields:
        additionalProperties:
          type: object
          description: 'Custom fields of the Account object. The name of each custom field
            has the form <code>*customField*__c</code>. Custom field names are case sensitive.
            See [Manage Custom Fields](https://knowledgecenter.zuora.com/BB_Introducing_Z_Business/Manage_Custom_Fields)
            for more information.
    
            '
        description: 'Container for custom fields of an Account object.
    
          '
        title: accountFieldsCustom
        type: object
    
  6. After ensuring that every occurrence of additionalProperties has type specified, save the file.

    Note: If you are using version 2.3.0 or later of swagger-codegen, you will need to remove the additionalProperties blocks entirely. See this post for more information.

  7. Use swagger-codegen to generate a C# library.

 

Manually modifying the Swagger spec can be time-consuming, so I’d recommend that you automate the process via a script. Don’t hesitate to reply to this post if you’d like any advice about doing that.

Zuora Documentation

Re: Working with a query in .Net with REST API

As of April 2019, we are providing a separate Swagger spec that has type specified for every occurrence of additionalProperties. It is available at:

https://assets.zuora.com/zuora-documentation/swagger-codegen-workaround.yaml

 

Update 5 May, 2019: We plan to provide this separate Swagger spec until June 2019.

 

See this Community post for more information.

Re: Working with a query in .Net with REST API

Hi David,

 

One of our customer is looking into this thread and following the instructions. Here is what they reported. Any help to debug would be appreciated.

 

===================

I am trying to get to additional properties on few objects (for now I am trying to get ZObject to work so I can at least get results from custom queries).

I was following steps described in this forum thread: https://community.zuora.com/t5/API/Working-with-a-query-in-Net-with-REST-API/m-p/25110/highlight/tru...

I’ve altered the yaml file to have zObject like this:

  zObject:

    additionalProperties:

      type: object   

      description: 'Field of the object.

 

        '

    type: object

 

I saved altered yaml into swagger-modified.yaml

Then I ran generation of the client using following command:

java -jar swagger-codegen-cli.jar generate -l csharp -DtargetFramework="v5.0" -DpackageName="Zuora.Client" -DnetCoreProjectFile=true  -o generated -i swagger-modified.yaml

However when I check the file generated for ZObject it doesn’t seem to be different.

 

The swagger-codegen is the latest version as of 2 days ago.

Zuora Documentation

Re: Working with a query in .Net with REST API

Hi @vasudeva_shenoy, do you know whether your customer is using the latest version of the 2.x branch of swagger-codegen or the latest version of the 3.x branch of swagger-codegen?

 

Right now the latest versions are 2.4.5 and 3.0.8 (see here), but csharp support in 3.0.8 is listed as “experimental”. And as far as I’m aware, we haven’t verified whether our Swagger spec is compatible with the 3.x branch of swagger-codegen.

 

Assuming your customer isn’t wanting to use the 3.x branch of swagger-codegen, I’ve got a couple of suggestions for your customer:

 

  • There is a known bug in version 2.3.0 and later of swagger-codegen that affects ZObject (see here). Would it be possible to use an earlier version of swagger-codegen?

    • If you can use an earlier version than 2.3.0 - You should set type: object within additionalProperties as explained earlier in this thread. This will ensure that the ZObject class extends Dictionary<String, Object> in ZObject.cs:

      public partial class ZObject : Dictionary<String, Object>,  IEquatable<ZObject>, IValidatableObject
      

      Otherwise, ZObject won’t extend Dictionary correctly:

      public partial class ZObject : Dictionary<String, >,  IEquatable<ZObject>, IValidatableObject
      

      Per my previous post in this thread, you can grab a Swagger spec that already includes the workaround from https://assets.zuora.com/zuora-documentation/swagger-codegen-workaround.yaml.
      Update 5 May, 2019: We plan to provide this Swagger spec until June 2019.

    • If you can’t use an earlier version than 2.3.0 - additionalProperties will cause compilation errors, so you’ll need to remove additionalProperties. You could replace additionalProperties by all the properties that you anticipate needing to reference in the ZObject class. For example, if you’re going to use Query to always query fields called Name and Status, you could modify the Swagger spec to look like this:

      zObject:
        type: object
        properties:
          Name:
      	  type: string
          Status:
      	  type: string
      

      This workaround is not ideal from a flexibility point of view, but it could be your best option.

  • As an alternative suggestion, I am aware of a fork of swagger-codegen 2.x called openapi-generator. I don’t know what the results are like, but would be interested to know whether it improves upon swagger-codegen in terms of compatbility with our Swagger spec.

 

Would any of these suggestions help you to make progress?