guardrail icon indicating copy to clipboard operation
guardrail copied to clipboard

sharing definitions between APIs

Open dsilvasc opened this issue 5 years ago • 4 comments

What's the recommended way to have two different APIs share some data definitions with Guardrail?

Swagger allows extracting definitions into their own files and referencing them with $ref. For multiple swagger specs (multiple APIs) to share the same definitions file, would it be possible to run Guardrail 3 times -- once to generate just definitions, then once per swagger spec to generate the server code, but skipping codegen for definitions? Or maybe generating definitions twice but in the same namespace?

dsilvasc avatar Mar 28 '19 21:03 dsilvasc

We generate the same definitions twice in the same package, as you suggested. #77 tracks a more broad feature, where we attempt to recognize identical objects created in different files in the same run (or possibly across runs, by using some sort of cache), but for now you can just specify the same package and it will overwrite with the same contents.

blast-hardcheese avatar Mar 28 '19 22:03 blast-hardcheese

How do you put a subset of definitions from two apis in the same package? It looks like x-scala-package only applies to paths.

dsilvasc avatar Mar 28 '19 22:03 dsilvasc

@dsilvasc since definitions are always children of the configured package (in build.sbt) we just put them in the same root package. x-scala-package can then be used to restructure the generated clients and servers relative to the package root.

blast-hardcheese avatar Mar 28 '19 22:03 blast-hardcheese

I think that leads to a potential problem.

Suppose api1.yaml has a definition for OwnDef and a reference into defs.yaml with a definition for SharedDef. Also api2.yaml has a definition for OwnDef (a different one) and a reference into defs.yaml as well.

If we generate code for api1.yaml and api2.yaml in the same package root, we'll have conflicting class files for the two OwnDef definitions.

dsilvasc avatar Nov 25 '19 18:11 dsilvasc