How to make simple POJO with openapi-generator-cli

239 views Asked by At

I am tying to use openapi-generator-cli to make the model (pojo) classes from a json api definition. It works, but it makes more than I want.

I am using -g java --global-property models as options, and this kind of works. It is only making the model classes now, and not all the API classes. However, it' still putting import in for things that are not found or used, for example;

Some imports are included which are not used, such as;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

Which is ok because I am using that elsewhere.

However, some of the code in the models are importing code I do not have, which it not generated, nor do I want;

import org.openapitools.client.JSON;

public static AmendRequestPiecesInner fromJson(String jsonString) throws IOException {
    return JSON.getGson().fromJson(jsonString, AmendRequestPiecesInner.class);
  }

I literally JUST want the simple POJO classes, with no extra things.

Is this possible?

1

There are 1 answers

0
gebi On

There is no simple option in OpenAPI Generator to achieve what you want.1

You can however use e.g. OpenAPI's "file post-processing" or "templating" features to adapt the generated code for your needs:

  • "file post-processing" passes each generated file to a script/application of your choice. So you can e.g. write a small sed/python/... command to remove the source code parts that you don't need.
  • "templating" let's you adapt the templates that OpenAPI Generator uses to generate the output files. By removing from the templates the parts that you don't need, you can make OpenAPI Generator generate only the source code that you need.

1 As you can check here, e.g. the code return JSON.getGson().fromJson( mentioned in OP's question will always be part of the output, as there are absolutely no conditionals around it.