Autofix for VocaDB API Java Client generated by OpenAPI
Just go to my sample VocaDB OpenAPI Java Client, and no need to read the rest of this doc ๐
-
install OpenAPI (see instruction)
- optionally you can use docker to run OpenAPI generator, see instruction
-
grab the
swagger.json
from VocaDB API page -
open bash and run these command
openapi-generator-cli generate \ --api-package <your package>.api \ --model-package <your package>.model \ --invoker-package <your package>.client \ -i <location of the vocadb swagger.json file> \ --group-id <your group-id> \ --artifact-id <your artifact-id> \ --artifact-version <your version> \ -g java \ -p java8=true \ -p dateLibrary=java8-localdatetime \ -p useRuntimeException=true \ -p useJakartaEe=true \ -p openApiNullable=false \ -p serializationLibrary=jackson \ -p licenseName=<your license name> \ -p licenseUrl=<your license url> \ --library <any candidates other than okhttp-gson> \ -o /local/sample-unfixed-client
- available library candidates are specified in OpenAPI Java page
- if you are using docker, make sure you modified the generator launch command,
properly mounted the output directory,
and modify the
-o
option to fit docker's environment
-
(if using
resttemplate
orwebclient
) resolve import conflict inUserApiApi.java
- remove
import <your package>.model.MediaType;
- let
apiUsersCurrentAlbumsAlbumIdPost()
andapiUsersCurrentAlbumsAlbumIdPostWithHttpInfo()
take the parameter of type<your package>.model.MediaType
- remove
-
(Optional) update some dependencies used by the client
- spring and Jackson related dependencies
- remove
jackson-databind-nullable
- copy LICENSE, etc.
- clone this project
git clone --recurse-submodules -j8 https://github.com/CXwudi/vocadb-openapi-java-client-autofixer
- open the my-config.yml file in
./vocadb-apiclient-fixer
and edit it- input directory is where your auto-generated client is
- can be relative path from the
./vocadb-apiclient-fixer
perspective
- can be relative path from the
- relative path to model is the relative path from the input directory to path of
--model-package
on youropenapi-generator-cli
command
- input directory is where your auto-generated client is
- cd into
./vocadb-apiclient-fixer
, run./mvnw spring-boot:run '-Dspring-boot.run.arguments="--spring.config.import=file:my-config.yml"'
- your client should be ready to use ๐
- OpenAPI is very fragile, so better stick with the command in the step to generate your client
- Gson is not working for parsing date, like
Expected BEGIN_OBJECT but was STRING at line 1 column 109 path $.items[0].song.createDate
- when using Jackson, must put
-p openApiNullable=false
, otherwise parsing issue on list withcom.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of 'org.openapitools.jackson.nullable.JsonNullable<java.util.List<cx.mikufan.vocadbapiclient.model.SongInListForApiContract>>' out of START_ARRAY token
. This is because withopenApiNullable=true
, your list isJsonNullable<List<>>
instead ofList<>
- as for data library, use
java8-localdatetime
- the default one
threetenbp
has parsing issue - the
java-8
one useZoneDateTime
which requires an additional zone info in the string, but vocadb's date doesn't have it
- the default one
- the my-config.yml file has some default config values filled in. It is used to autofix the sample-unfixed-client for demo purposes.
- In fact, the sample VocaDB OpenAPI Java Client is fixed by this demo config
see notes in my sample client
This fixer will change all enum classes with suffixname of s
into a class that can make Jackson JSON parser works for one-line multiple-value enum string.
For example, take a look at the SongForApiContract
in the sample-unfixed-client.
It has a field private PVServices pvServices;
.
Before fixing, PVServices
is just an enum class. It can only accept one value for that field.
However, in reality, VocaDB could return multiple values for that field, separated by comma (like pvServers: "NicoNicoDouga, Youtube"
).
So the fixer will change each enum class like PVServices
into a normal class that can accept such multiple values of the same enum.
And each new class will have customized @JsonCreator
and @JsonValue
annotations to stay compatible with the behavior of the old unfixed enum class.
This fixer also globally replaces all strings ApiApi
to Api
.
If a file name contains such a string, the file is also renamed.