guardrail
guardrail copied to clipboard
InvariantFailedException on swagger inheritance
Hi,
I have a swagger spec where the definitions include inheritance like this minimal example:
---
swagger: "2.0"
info:
version: "1.0.1"
basePath: "/v1"
paths:
definitions:
A:
type: "object"
B:
allOf:
- $ref: "#/definitions/A"
- type: "object"
C:
allOf:
- $ref: "#/definitions/B"
- type: "object"
Running guardrail 0.43.0 with that spec crashes with:
Error: Error attempting to process swagger.yaml:
org.scalameta.invariants.InvariantFailedException: invariant failed:
when verifying value.!=(null)
found that value is equal to null
where value = null
at com.twilio.guardrail.generators.CirceProtocolGenerator$PolyProtocolTermInterp$.apply(CirceProtocolGenerator.scala:395)
at com.twilio.guardrail.generators.CirceProtocolGenerator$PolyProtocolTermInterp$.apply(CirceProtocolGenerator.scala:364)
...
at com.twilio.guardrail.generators.AkkaHttp$.apply(AkkaHttp.scala:32)
at com.twilio.guardrail.generators.AkkaHttp$.apply(AkkaHttp.scala:14)
...
at com.twilio.guardrail.core.CoreTermInterp$$anon$1.$anonfun$apply$37(CoreTermInterp.scala:129)
...
at com.twilio.guardrail.ReadSwagger$.readSwagger(ReadSwagger.scala:24)
at com.twilio.guardrail.CLICommon$.$anonfun$run$8(CLI.scala:69)
...
at com.twilio.guardrail.CLICommon$.run(CLI.scala:67)
at com.twilio.guardrail.CLICommon$$anonfun$handleLanguage$1.$anonfun$applyOrElse$1(CLI.scala:128)
at com.twilio.guardrail.CLICommon$$anonfun$handleLanguage$1.$anonfun$applyOrElse$1$adapted(CLI.scala:128)
at com.twilio.guardrail.CLICommon.main(CLI.scala:133)
at com.twilio.guardrail.CLICommon.main$(CLI.scala:131)
at com.twilio.guardrail.CLI$.main(CLI.scala:137)
at com.twilio.guardrail.CLI.main(CLI.scala)
@dsilvasc Thanks for the report!
This seems to be because the allOf
handling assumes a type hierarchy (Pet
>: Cat
>: Siamese
) based on the discriminator
field.
The required change would be to wrap https://github.com/twilio/guardrail/blob/aee9798633ad9f9be55f7a3445e63744233b41b2/modules/codegen/src/main/scala/com/twilio/guardrail/ProtocolGenerator.scala#L128 in Option
, then follow the compiler errors, eventually getting to the point where https://github.com/twilio/guardrail/blob/aee9798633ad9f9be55f7a3445e63744233b41b2/modules/codegen/src/main/scala/com/twilio/guardrail/generators/CirceProtocolGenerator.scala#L406-L414 can just be reduced to the underlying single object decoder.
CC @stanislav-chetvertkov, as this will likely impact #54