https://kotlinlang.org logo
#http4k
Title
# http4k
e

Elizabeth Thomas

02/18/2021, 4:57 AM
@dave Is it possible to add the
OpenApiExtension
for the
paths
in OpenApi spec through http4k-contract? My scenario is that I want to be able to add a custom attribute with a value to every path in my OpenApi Spec. I was able to add a custom attribute block using OpenApiExtension to the
extensions
field in
OpenApi3
. But I am not sure how to do it for each of my path object
Copy code
apiBaseUrl bind contract {
      renderer = OpenApi3(
        ApiInfo(apiName, apiVersion, apiName),
        WdsHttpJackson,
        listOf(XApiDefinitionExtension),
        OpenApi3ApiRenderer(WdsHttpJackson)
      )
      descriptionPath = swaggerJsonPath
      routes += contractRoutes
    }
I want my rendered OpenAPI Spec to have the
paths
object something like this
Copy code
...
"paths": {
  "/abc": {
    "x-custom-classification": "INTERNAL",
    "get": {
       ...
     }
  },
  "/def": {
    "x-custom-classification": "PUBLIC",
    "get": {
       ...
     }
  }
}
...
Is there a way for me to inject
x-custom-classification
attribute to the open api spec for every path using OpenApiExtensions?
I am seeing that the
Path Item Object
in OpenAPI3 Spec MAY be extended with
OpenAPI Extensions
d

dave

02/18/2021, 2:36 PM
yes - the extensions each receive access to the entire document structure. you can do whatever you like with them.. 🙂
e

Elizabeth Thomas

02/18/2021, 3:15 PM
So when I am defining my route, how do I inject the extension or create a route that extends from the extension?
Copy code
"/echo" meta {
        summary = "echoes the name and message sent to it"
        receiving(body to NameAndMessage("jim", "hello!"))
        returning(OK, body to NameAndMessage("jim", "hello!"))
    } bindContract POST to { request: Request ->
        val received: NameAndMessage = body(request)
        Response(OK).with(body of received)
    }
Do you have any example?
d

dave

02/18/2021, 3:16 PM
you can't inject an extension into the routes currrently. you have to inject them into the OpenApi renderer
and they operate on the entire node structure of the document
the example is in the tests. Look at OpenApi3Test and you can see AddSimpleFieldToRootNode . The output can be seen at
http4k-contract/src/test/resources/org/http4k/contract/openapi/v3/OpenApi3AutoTest.renders as expected.approved
👍 1
e

Elizabeth Thomas

02/18/2021, 3:45 PM
Thank you @dave - taking a look!
So my example also followed a similar pattern and I was able to see a custom block of object getting added at the root of the node. Do I have to add some conditionals in the extension class if I need the custom block to be added only at certain path levels?
d

dave

02/18/2021, 4:37 PM
yes, you need to implement the entire logic piece to navigate to where you need it
e

Elizabeth Thomas

02/18/2021, 4:43 PM
Oh my, I thought it would be easy 🙂 Now I will have to debug and add the conditionals for the JSON Node
4 Views