Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
implements BeanValidationFeatures, PerformBeanValidationFeatures, GzipFeatures {

static final String MEDIA_TYPE = "mediaType";
private static final int GSON_ADAPTERS_PER_BATCH = 200;

private final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);

Expand Down Expand Up @@ -1415,6 +1416,31 @@ public void forceSerializationLibrary(String serializationLibrary) {
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
generateYAMLSpecFile(objs);
if (isLibrary(OKHTTP_GSON)) {
// Match the models registered by JSON.mustache, preserving their generation order.
@SuppressWarnings("unchecked")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comments explaining what this new block code does. thank you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment explaining that adapter registrations are split into batches to avoid exceeding the JVM method size limit. The original registration order is preserved.

List<ModelMap> models = (List<ModelMap>) objs.get("models");
List<CodegenModel> adapters = new ArrayList<>();
if (models != null) {
for (ModelMap model : models) {
CodegenModel m = model.getModel();
if (m != null && !m.isEnum && !m.hasChildren) {
adapters.add(m);
}
}
}
// Large initializers can exceed the JVM method-size limit; register in bounded batches.
if (adapters.size() > GSON_ADAPTERS_PER_BATCH) {
List<Map<String, Object>> batches = new ArrayList<>();
for (int i = 0; i < adapters.size(); i += GSON_ADAPTERS_PER_BATCH) {
Map<String, Object> batch = new HashMap<>();
batch.put("index", batches.size());
batch.put("models", adapters.subList(i, Math.min(i + GSON_ADAPTERS_PER_BATCH, adapters.size())));
batches.add(batch);
}
objs.put("gsonAdapterBatches", batches);
}
}
return super.postProcessSupportingFileData(objs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ public class JSON {
return clazz;
}

{{#gsonAdapterBatches}}
private static void registerModelAdapters{{index}}(GsonBuilder gsonBuilder) {
{{#models}}
gsonBuilder.registerTypeAdapterFactory(new {{modelPackage}}.{{{classname}}}.CustomTypeAdapterFactory());
{{/models}}
}

{{/gsonAdapterBatches}}
static {
GsonBuilder gsonBuilder = createGson();
gsonBuilder.registerTypeAdapter(Date.class, dateTypeAdapter);
Expand All @@ -131,6 +139,10 @@ public class JSON {
gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter);
{{/jsr310}}
gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter);
{{#gsonAdapterBatches}}
registerModelAdapters{{index}}(gsonBuilder);
{{/gsonAdapterBatches}}
{{^gsonAdapterBatches}}
{{#models}}
{{#model}}
{{^isEnum}}
Expand All @@ -140,6 +152,7 @@ public class JSON {
{{/isEnum}}
{{/model}}
{{/models}}
{{/gsonAdapterBatches}}
gson = gsonBuilder.create();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ public class JavaClientCodegenTest {

private static final String JAVA_GENERATOR = "java";

@Test
@SneakyThrows
public void testOkHttpGsonBatchesModelAdapterRegistrations() {
Path output = newTempFolder();
Path spec = output.resolve("many-models.yaml");
StringBuilder yaml = new StringBuilder("openapi: 3.0.0\ninfo: {title: Batch test, version: '1'}\npaths: {}\ncomponents:\n schemas:\n");
for (int i = 0; i < 201; i++) {
yaml.append(" Model").append(i).append(":\n type: object\n properties:\n value: {type: string}\n");
}
Files.write(spec, yaml.toString().getBytes(StandardCharsets.UTF_8));
CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(JAVA_GENERATOR)
.setLibrary(OKHTTP_GSON)
.setInputSpec(spec.toString())
.setOutputDir(output.toString());
new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
Path json = output.resolve("src/main/java/org/openapitools/client/JSON.java");
String code = new String(Files.readAllBytes(json), StandardCharsets.UTF_8);
StaticJavaParser.parse(code);
assertThat(code).contains("private static void registerModelAdapters0(GsonBuilder gsonBuilder)",
"private static void registerModelAdapters1(GsonBuilder gsonBuilder)",
"registerModelAdapters0(gsonBuilder);", "registerModelAdapters1(gsonBuilder);");
for (int i = 0; i < 201; i++) {
assertThat(code).containsOnlyOnce("new org.openapitools.client.model.Model" + i + ".CustomTypeAdapterFactory()");
}
}

// This is the kind of information that ideally would be defined and available system-wide
@Getter
enum Library {
Expand Down
Loading