Repository navigation
Conversation
There was a problem hiding this comment.
1 issue found across 36 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/model/MyExampleGet200Response.java">
<violation number="1" location="samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/model/MyExampleGet200Response.java:173">
P2: This typed array constructor rejects an otherwise valid empty array. Update `setActualInstance` to accept empty lists (while validating elements for non-empty lists), so `Collections.emptyList()` and JSON `[]` work consistently with the schema.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| setActualInstance(o); | ||
| } | ||
|
|
||
| public MyExampleGet200Response(List<@Valid OneOf1> o) { |
There was a problem hiding this comment.
P2: This typed array constructor rejects an otherwise valid empty array. Update setActualInstance to accept empty lists (while validating elements for non-empty lists), so Collections.emptyList() and JSON [] work consistently with the schema.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/model/MyExampleGet200Response.java, line 173:
<comment>This typed array constructor rejects an otherwise valid empty array. Update `setActualInstance` to accept empty lists (while validating elements for non-empty lists), so `Collections.emptyList()` and JSON `[]` work consistently with the schema.</comment>
<file context>
@@ -170,6 +170,16 @@ public MyExampleGet200Response(Object o) {
setActualInstance(o);
}
+ public MyExampleGet200Response(List<@Valid OneOf1> o) {
+ this();
+ setActualInstance(o);
</file context>
There was a problem hiding this comment.
Not changing this here. The rejection comes from setActualInstance, which has refused empty lists since before this PR. The new constructor just calls it, so new MyExampleGet200Response((Object) List.of()) fails the same way on master. Accepting [] also needs a rule for unions with two list members, which both match it. That changes every okhttp-gson union, so it's better as a separate PR.
Sekator778
left a comment
There was a problem hiding this comment.
Built it, ran JavaClientCodegenTest and regenerated the okhttp-gson samples; all match and compile. A member model named Nullable compiled on master and fails here on isNullable(). Could the check skip that name, as the constructors skip clashes?
… test any-type members
|
@Sekator778 thanks for building and checking the samples. Fixed in the latest push: a member named |
Summary
okhttp-gsononeOf/anyOfmodels only have anObjectconstructor and agetActualInstance()that returnsObject, so nothing in the API says which types a union accepts, and reading a value takes aninstanceofcheck against each member. Thenativelibrary already generates one constructor per member. This adds the same constructors tookhttp-gson, plus anis<Member>()check next to eachget<Member>().For example, a pet status that accepts the known values and any status the server adds later:
The generated additions for
OpenPetStatus:public class OpenPetStatus extends AbstractOpenApiSchema { public OpenPetStatus(Object o) { ... } + public OpenPetStatus(PetStatus o) { this(); setActualInstance(o); } + public OpenPetStatus(String o) { this(); setActualInstance(o); } public PetStatus getPetStatus() throws ClassCastException { ... } + public boolean isPetStatus() { return super.getActualInstance() instanceof PetStatus; } public String getString() throws ClassCastException { ... } + public boolean isString() { return super.getActualInstance() instanceof String; }Members skip what would clash or mislead:
List<Integer>afterList<BigDecimal>) gets none, as innative(x-duplicated-data-type). Free-form and any-type members get none either, because their type isObjectand the existing constructor already covers them.is<Member>(): container members get none, becauseinstanceof Listcan't tellList<Pet>from another list. Free-form and any-type members get none either, because every value is anObject. A member namedNullablegets none, becauseAbstractOpenApiSchemaalready declaresBoolean isNullable().The
Objectconstructor stays, so existing code keeps compiling, and a wrong type still fails at runtime insetActualInstanceas before.Evidence
"available"reads asPetStatus.AVAILABLE,"adopted"reads as the string, and both write back unchanged.testOkHttpGsonOneOfAnyOfTypedConstructorswith spec3_0/java/okhttp-gson-composed-typed-constructors.yaml, covering enum and string, numeric and boolean, model and list, same-erasure lists, free-form, any-type and nullable members, and a member model namedNullable.JavaClientCodegenTest: 321 passed.new Scalar(5)isisInteger(),new Scalar(5L)isisLong(), andnew PetOrPets(List.of(pet)).isPet()isfalse../bin/generate-samples.sh ./bin/configs/*.yamlchanges 32 files in 5okhttp-gsonsamples, all of them additions. All 5 samples passmvn test-compile.Merge Danger
Door: two-way. Templates only.
Blast Radius: okhttp-gson unions.
Additive, with one source-compatibility edge: when a model has two or more typed constructors,
new X(null)with an untypednullno longer compiles because the call is ambiguous.new X()or a cast such as(String) nullworks.PR checklist
@bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg @KannaKim: review requested for the Java
okhttp-gsontemplate change.