[java] fix: okhttp-gson allOf children of a oneOf/anyOf parent do not compile with additionalProperties

Merged
WI
10d ago
+109-1

Follow-up to #24895, fixing the regression @Sekator778 reported there.

With disallowAdditionalPropertiesIfNotPresent=false, #24895 tags every model whose parentModel chain has isAdditionalPropertiesTrue with x-inherits-additional-properties. A discriminated oneOf/anyOf parent counts as such an ancestor, but okhttp-gson renders it with oneof_model/anyof_model: it extends AbstractOpenApiSchema and declares no bag. So an allOf child of it (ExtendedPet allOf Pet, Pet oneOf Cat|Dog) gets an @Override putAdditionalProperty calling super, with nothing to override:

ExtendedPet.java:[115,2] error: method does not override or implement a method from a supertype
ExtendedPet.java:[117,9] error: cannot find symbol

This compiled on 7.25.0 and fails on 7.26.0.

The fix

JavaClientCodegen.postProcessAllModels stops walking up the chain at a oneOf/anyOf model. Nothing is inherited through such a model, so the first pojo below it declares the bag again, and its own descendants inherit it as before.

Tests

  • JavaClientCodegenTest#testAdditionalPropertiesFieldIsDeclaredBelowOneOfAndAnyOfParentsForGson, using the new 3_0/java/okhttp-gson-additional-properties-composed-parent.yaml, covers a oneOf parent and an anyOf parent. It also checks a grandchild (ExtendedPetLeaf extends ExtendedPet) that still inherits the bag.
  • I generated that spec with the CLI and compiled it with mvn compile. On master it fails with the flag on (the errors above, in ExtendedPet, ExtendedPetLeaf and ExtendedShape). With this PR it compiles, both with the flag on and with the default.
  • I regenerated every bin/configs/java-okhttp-gson*.yaml sample. Apart from the version stamp, there are no diffs, so no sample is touched.

Known gaps (pre-existing, not changed here)

While checking this at runtime, I found that subclassing a oneOf/anyOf wrapper in okhttp-gson has problems that exist with the flag off too:

  • If the child does not redeclare the discriminator property, it doesn't compile: its constructor assigns this.petType, which the wrapper doesn't have.
  • The wrapper's isNullable and schemaType fields are serialized into the child's JSON. The child's validateJsonElement then rejects that JSON, so fromJson(toJson()) throws.

These deserve their own fix. This PR only restores what compiled on 7.25.0.

PR checklist

  • Read the contribution guidelines.
  • Built the project and regenerated the okhttp-gson samples (no changes).
  • Technical committee: @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg

Generated with Claude Code


Summary by cubic

Fixes the okhttp-gson regression from #24895 where allOf children of a discriminated oneOf/anyOf parent fail to compile with disallowAdditionalPropertiesIfNotPresent=false.

  • The parent wrapper extends AbstractOpenApiSchema and declares no additionalProperties bag, so the child's @Override putAdditionalProperty and super calls had nothing to bind to.
  • The ancestor walk in JavaClientCodegen.postProcessAllModels now stops at oneOf/anyOf models; the first pojo below them declares the bag again.
  • Adds a test spec covering oneOf and anyOf parents plus a grandchild that still inherits the bag. Regenerated okhttp-gson samples have no diffs.

Known gaps

Pre-existing and unchanged: subclassing a oneOf/anyOf wrapper has runtime problems even with the flag off โ€” a child that doesn't redeclare the discriminator property doesn't compile, and serializing the wrapper's isNullable/schemaType fields makes fromJson(toJson()) throw. Those deserve separate fixes.