Handling shared models #5264
Replies: 2 comments 2 replies
-
Hi @adamvandenhoven Something that wasn't clear to me from reading the question: are all those APIs on the same host / fqdn? If so why not simply generating/authoring an aggregated document? Assuming the models are truely identical on the backend (not a facet design pattern) and they are on different hosts, there's no way to reduce duplication today. In my opinion that's mostly a good thing. Generated clients are infrastructure services (DDD) and you generally will want to interface them with more central layers. So having potentially duplicated models that belong to different services is not necessarily a bad thing in that context. That being said, it might be important for some scenarios. You might be able to get around this limitation by doing something similar to what we do in TypeScript:
This will require a bit of scripting but should eventually work. Let us know if you have additional comments or questions. |
Beta Was this translation helpful? Give feedback.
-
I can conceive of many different ways to make do with what Kiota already does; for example, I could use C# operator overloading to create implicit casts between the different types. If kiota already handles external schema references, then I have to assume that you're also parsing and honouring the |
Beta Was this translation helpful? Give feedback.
-
I don't know if a solution to this already exists, but I couldn't find anything obvious from my searching around, so I'm assuming its a new idea.
I have a number different apis that are all related but independent. In general, they have common schemas; for example several have
Address
schemas that are all the same.If I happen to need two different APIs, both of which provide an Address, Kiota will create two different models, which just happen to be the same. If I want to write
GeocodeAddress
I'll either need two methods, one for each model, or I can use a partial to give them a common interface or super class (which means I'm manually repeating the same properties a third time). Neither is particularly joy inducing.One thing that the Open API explicitly says is "An OpenAPI document MAY be made up of a single document or be divided into multiple, connected parts at the discretion of the user." The
$ref
, while typically just a fragment pointing to something in the current doc (#/components/schemas/Address
), it can be a full url.Given that
x-ms-kiota-info
can provide language level information (much of which is also settable as cli arguments), specifically the clientNameSpace, when kiota encounters"$ref": "https://api2.example.com/oas.yaml#/components/schema/Address"
it could parse that and minimally add Address to the normal Models folder but with the namespace defined in the source schema doc. This way if I have two Apis that reference the same common schema, the partials will merge with no negligible impact (at least for C# and those that can handle partials).Another solution (probably a more easily implemented) might be to retrieve and parse the linked schema for its namespace and if it exists, simply use the model name with the correct namespace and emit a message on completion like the prompt to run
kiota info
to runkiota generate
for the referenced schemas (it may require the sorts of changes discussed in #4835 to work)Because I may not have control over the referenced schemas, there is no guarantee that a
x-ms-kiota-info
will be defined in the referenced schema, an additional extension might be needed to provide this information something like:It occurs to me (yes, I'm creating solutions as I write) that this would allow you to generate the client without having to load and parse the referenced schema file since you can properly infer the model name from the schema path. Moreover, since I know that API2 has published its client as a nuget, I don't need kiota to generate one for me just to get its models, I can flag that with
pregenerated
and instead of saying "go generate this schema" you just include it with the info commands.This last idea, using
x-ms-kiota-dependancies
might be the best solution to "how do I share models between apis" since its presence can trigger the behaviour, it wouldn't require any additional http requests (a potentially big problem is api1 references api2 which references api3....) and it should let circular references just work?Beta Was this translation helpful? Give feedback.
All reactions