Jeffrey Knight
Jeffrey Knight

Reputation: 5975

API Design: Expressing search criteria in XML

Last year, my group developed a web service that included basic search functionality. All search conditions where combined with a boolean AND:

<conditions>
  <condition name="name1">value1</condition>
  <condition name="name2">value2</condition>
<conditions>

... is equivalent to name1=value1 AND name2=value2 etc.

Now, we have been asked to expand the search feature to allow for more complex searches. I see two plausible approaches:

OPTION #1: Let users pass in their own SQL query (either full clause, or just the 'where).

Examples:

<where>Cost = 5000.00 OR Cost > 5000.00</where>
<query>SELECT cmis:name FROM cmis:document WHERE cmis:name LIKE '%test%'</query>

Precedent:

Advantages:

Disadvantages:

OPTION #2. Revamp our <conditions> approach to allow more granular queries in the soap request.

Example (from FetchXML):

 <filter type='and'> 
     <condition attribute='lastname' operator='ne' value='Cannon' /> 
 </filter> 

Precedent:

Advantages:

Disadvantages:

I hope that the examples, precedent, advantages, and disadvantages give enough background to avoid subjective answers. I am looking for answers grounded in standards and best practices.

My question is: are there definitive reasons for choosing one approach over the other in expanding an API?

Upvotes: 2

Views: 1791

Answers (2)

Alvin
Alvin

Reputation: 10458

I agree with DWRoelands on Option #1 is probably a bad idea from security perspective.

I would suggest an Option #3 that is similar to your Option #2 but use a DSL(Domain Specific Language). So you will have something like:

<condition expression="$firstname='John' and $lastname !='Doe'"/>

The server will then need to have a parser to compile and run the expression. You are free to design the syntax of the expression to suit your needs.

I have personally implemented your option #2 and the DSL before. I like DSL better because of it's flexibility and it makes my XML looks cleaner. You're right that this approach will require more server-side coding, but I prefer to do more work than letting user do more work.

Upvotes: 2

DWRoelands
DWRoelands

Reputation: 4940

Option #2, if only for one reason: security.

Allowing end users to pass arbitrary SQL to your database is an invitation to disaster. You either trust your users to NEVER make mistakes in SQL, or you have to write code to determine which SQL you're going to accept and which SQL you're going to reject.

Option #2 will be harder to design and implement, but option #1 guarantees that you will hate yourself at some point when some user updates every record in an important table.

Upvotes: 3

Related Questions