Repository navigation
Description
Is your feature request related to a problem? Please describe.
For a long time, OpenAPI has not supported the specification of inter-parameter dependencies, even though there's an open issue requesting support for it. This issue has become the most upvoted of all times in the OpenAPI repository.
An example of an inter-parameter dependency is "if parameter p1 is used, then p2 must be set to 'A'".
If these dependencies could be expressed in a machine-readable way, it would be possible to automate lots of things, including the generation of the source code in charge of validating these dependencies in clients and servers.
We have recently proposed an alternative to specify and automatically analyze these dependencies. It is all summed up in the following two Medium posts, for if you have interest:
- Inter-parameter dependencies in REST APIs (5-min read).
- Handling inter-parameter dependencies in REST APIs with IDL4OAS (7-min read).
With our proposal, it is possible to specify inter-parameter dependencies using IDL, a DSL specifically tailored for expressing inter-parameter dependencies in web APIs. What I would like to propose in this feature request is to automatically generate built-in assertions in the source code (client or server, any programming language) based on the dependencies expressed in IDL.
Describe the solution you'd like
We have implemented IDL as an Xtext DSL. Since it's written in Java, and so is OpenAPI generator, I think we could use the IDL parser for generating the assertions in the code, based on the IDL dependencies. For example, if we found a dependency like the following one:
ZeroOrOne(p1, p2);
We would write the following assertion in a Java server:
if (p1 != null && p2 != null)
throw new BadRequestException("Parameters p1 and p2 cannot be used together");
// Otherwise, continue business logicAdditional context
The two Medium posts from above summarize pretty much everything there's to know about inter-parameter dependencies. Here's a list of some other resources that you may find interesting:
- IDL repository, including examples of IDL dependencies: https://github.com/isa-group/IDL
- IDL repository, to be used as a Maven dependency (e.g., in this project): https://github.com/isa-group/IDL-mvn-dep
- Research paper about the specification and analysis of these inter-parameter dependencies: https://www.researchgate.net/publication/348280988_Specification_and_Automated_Analysis_of_Inter-Parameter_Dependencies_in_Web_APIs
- Example of an OpenAPI specification using IDL ("x-dependencies" extension):
paths:
/example/route:
get:
operationId: exampleOperation
parameters:
- in: query
type: boolean
required: false
name: p1
- in: query
type: integer
required: false
name: p2
- in: query
type: string
required: false
name: p3
x-dependencies:
- Or(p1, p2, p3);
- IF p1==true THEN p2;
Activity
AML14 commentedon Feb 16, 2021
This issue may serve as a starting point for a discussion with the community. We (my team and I) are willing to implement the feature for some programming languages (e.g., Java and Python). But before starting, we would like to get your feedback on it.
For example:
[Java] Add generation of assertions from IDL dependencies (OpenAPIToo…
enriquebarba97 commentedon Jul 18, 2021
Hello.
I have implemented a first version for this issue that generates Java assertions from IDL, adding support for the
x-dependenciesextensionI added the necessary methods for processing IDL in the
AbstractJavaCodegenclass, adding the IDL expressions and generated assertions using thevendorExtensionsattribute.As an example, this is a Youtube API operation in OAS with its dependencies expressed in IDL (in the x-dependencies section):
This OAS generates the following assertions inside the Spring method that handles the request:
The generated assertions aren't pretty, but they are valid and do their job. I'm currently working on a second version that would reduce this assertions to:
The
OnlyOneDependencyandZeroOrOneDependencymethods (and alsoAllOrNoneDependencyandOrDependency, not included in this example) would be added to the generated project at static methods in an utility class.The code for the first version is available in the following repository, which includes the
youtube.yamlfile with the previous method and another two example methods.The generators supported in this demo are the following Java client library:
And the following servers:
To add more libraries and frameworks, you only have to add the assertion section to the appropriate place in the templates, but I don't have experience with most of the libraries, so I did not add them.
The idea would be to add this to the project as an experimental feature for Java and Python, and I would like to receive some feedback from the community about the currently implemented code. Thank you very much!! 😄
AML14 commentedon Jul 20, 2021
Thanks a lot for the work @enriquebarba97, this looks great. If this works well, I would suggest to open a PR following the contributing guidelines. But I also agree that it would be good to get some feedback before, either from the core developers of OpenAPI Generator, or from the community.
Since this feature currently targets specific versions of clients and servers for Java, it may be a good idea to reach out to the main Java developers of the project (listed here).
As Enrique says, future languages and frameworks should be easy to add, by taking Enrique's implementation as a starting point.
[Java] WIP: Generate more readable assertions for OpenAPITools#8722
enriquebarba97 commentedon Aug 6, 2021
I have finished an implementation that can be ready for a pull request. However, I still have some questions about some details on the implementation that I would like to receive feedback on.
To implement the processing of IDL, I added the necessary logic in the
AbstractJavaCodegenclass, adding to thefromOperationmethod. The specific flow is:x-dependenciesextension infromOperation.CodegenDependency, so they can be addressed in mustache.CodegenDependencyhas adependencyattribute with the IDL dependency to write in the exception messages, and anassertionattribute with the assertion in Java language.vendorExtensionssection in theCodegenOperationobject.The section added to the mustache templates looks like the following:
The assertions generated are:
IF condition THEN consequence) as(!(condition) || (consequence))in Java syntax.e.g. p1<p2).e.g. p1+p2<100).This is how the example from the previous comment would look like in a Spring server stub:
The feature is mostly finished, and the main code can be checked here, but I ran into some problems and I would like to get some feedback.
To add the static methods for the predefined dependencies, I added a
DependenciesUtil.mustachefile with the implementation of the methods. Right now, I generate the Java file by checking if thex-dependenciesextensions are present in the post-processing of the operation, and add the file to thesupportingFileslist (see here). This is implemented just forJavaClientCodegenfor now. However, I believe this is not the most appropriate way of doing this, so I would like some suggestions for thisJavaClientCodegenand other Java generators as well.Any other feedback about the code is also welcome. If everything is fine, my plan is to do some clean up and open a pull request sometime next week. I will mention some people from the team and the Java technical committee so this does not get lost. Thank you very much for your help! 😄
@wing328 @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @nmuesch
[Java] Finish dependencies feature and cleanup OpenAPITools#8722
foxpluto commentedon Dec 21, 2022
No news about the integration of this PR ?
I really would like to use this wonderful work in my API.