|
1 | 1 | # felles-openapi-customizer-lib |
2 | | -felles-openapi-customizer-lib eies og forvaltes av team-rettighet |
| 2 | + |
| 3 | +Takes an OpenAPI specification and customizes all paths with the standard |
| 4 | +security scheme and headers typically used in SPK. |
| 5 | + |
| 6 | +These headers are normally omitted from the OpenAPI specification for |
| 7 | +practical purposes and provided to the client and consumed by the server |
| 8 | +using implicit infrastructure libraries such as `felles-outbound-lib`. This |
| 9 | +class allows adding these headers back in, in order for the server to show a |
| 10 | +complete specification using SwaggerUI. |
| 11 | + |
| 12 | +There are two standard security schemes typically supported in SPK: |
| 13 | + |
| 14 | +1. Token based authentication using a security token. |
| 15 | +2. Basic authentication using the employee's username and password. |
| 16 | + |
| 17 | +There are three standard headers supported in SPK: |
| 18 | + |
| 19 | +1. `X-Application-Id` which contains the caller's application identity. |
| 20 | +2. `X-Correlation-Id` which correlates the invocation across systems. |
| 21 | +3. `X-Request-Origin` which contains the origin's application identity. |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +### Maven |
| 26 | + |
| 27 | +Add the following dependency to your project: |
| 28 | + |
| 29 | +```xml |
| 30 | +<!-- add this to your pom.xml --> |
| 31 | +<dependency> |
| 32 | + <groupId>no.spk.felles</groupId> |
| 33 | + <artifactId>felles-openapi-customizer-lib</artifactId> |
| 34 | + <version>0.0.1-SNAPSHOT</version> |
| 35 | +</dependency> |
| 36 | +``` |
| 37 | + |
| 38 | +### Customize an existing OpenAPI-spec (contract-first) |
| 39 | + |
| 40 | +Takes the filename of an OpenAPI specification on classpath as input, |
| 41 | +reads the file, customizes it with the given security schemes and |
| 42 | +headers, and returns it as an `OpenAPI` object. |
| 43 | + |
| 44 | +The `OpenAPI` object can be provided to Spring as a `@Bean` in order for |
| 45 | +documentation providers such as Springdoc to show the specification on |
| 46 | +the server using SwaggerUI. |
| 47 | + |
| 48 | +```kotlin |
| 49 | +@Bean |
| 50 | +fun createOpenAPI() { |
| 51 | + val customizer = OpenAPICustomizer( |
| 52 | + customizeWithSecuritySchemes = true, |
| 53 | + customizeWithStandardHeaders = true, |
| 54 | + ) |
| 55 | + return customizer.readAndCustomizeOpenAPI("/openapi/openapi.yaml") |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +Note that the above requires the OpenAPI specification to be unpacked |
| 60 | +into `/openapi/openapi.yaml` on the classpath. |
| 61 | + |
| 62 | +This is well-suited for OpenAPI specifications written contract-first, |
| 63 | +where the specification is available as a file. |
| 64 | + |
| 65 | +If using Springdoc, it is also advisable to disable scanning of packages |
| 66 | +adding `springdoc.packages-to-scan=none` to Spring Boot's configuration, |
| 67 | +otherwise annotated resources might show up twice in the specification. |
| 68 | + |
| 69 | +### Customize a generated OpenAPI-spec (code-first) |
| 70 | + |
| 71 | +Customizes an existing OpenAPI specification with the given security |
| 72 | +schemes and headers. |
| 73 | + |
| 74 | +The `OpenAPI` object can be provided to Spring as a `@Bean` in order for |
| 75 | +documentation providers such as Springdoc to show the specification on |
| 76 | +the server using SwaggerUI. |
| 77 | + |
| 78 | +```kotlin |
| 79 | +@Bean |
| 80 | +fun createOpenAPI() { |
| 81 | + val customizer = OpenAPICustomizer( |
| 82 | + customizeWithSecuritySchemes = true, |
| 83 | + customizeWithStandardHeaders = true, |
| 84 | + ) |
| 85 | + val generatedOpenAPI = OpenAPI() |
| 86 | + return customizer.customizeOpenAPI(generatedOpenAPI) |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +This is well-suited for OpenAPI specifications that are generated from |
| 91 | +annotated source code, after which these customizations are applied. |
| 92 | + |
| 93 | +## Development |
| 94 | + |
| 95 | +### Requirements |
| 96 | + |
| 97 | +Requirements to build the project on your machine: |
| 98 | + |
| 99 | +* JDK |
| 100 | +* Maven |
| 101 | + |
| 102 | +### Build |
| 103 | + |
| 104 | +Run the following command to build the project: |
| 105 | + |
| 106 | +```shell |
| 107 | +mvn clean install |
| 108 | +``` |
| 109 | + |
| 110 | +### Branch and release |
| 111 | + |
| 112 | +1. Branch from `main`. |
| 113 | +2. Create a pull-request and merge to `main`. |
| 114 | +3. This will release a new version. |
0 commit comments