@@ -115,10 +115,12 @@ function schemaTypeToJava(
115115 }
116116 // When exactly two non-null types and one of them is string, prefer String
117117 // over Object to avoid unnecessary type erasure on common wire-level unions
118- // (e.g., string | null, string | boolean). For wider unions keep Object.
118+ // (e.g., string | null, string | boolean). For string | object keep Object
119+ // so downstream code is not forced to cast. For wider unions keep Object.
119120 if ( nonNull . length === 2 ) {
120121 const hasString = nonNull . some ( ( s ) => typeof s === "object" && ( s as JSONSchema7 ) . type === "string" ) ;
121- if ( hasString ) {
122+ const hasObject = nonNull . some ( ( s ) => typeof s === "object" && ( s as JSONSchema7 ) . type === "object" ) ;
123+ if ( hasString && ! hasObject ) {
122124 return { javaType : "String" , imports } ;
123125 }
124126 }
@@ -308,7 +310,7 @@ async function generateSessionEventBaseClass(
308310 lines . push ( ` */` ) ;
309311 lines . push ( `@JsonIgnoreProperties(ignoreUnknown = true)` ) ;
310312 lines . push ( `@JsonInclude(JsonInclude.Include.NON_NULL)` ) ;
311- lines . push ( `@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = UnknownSessionEvent.class)` ) ;
313+ lines . push ( `@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true, defaultImpl = UnknownSessionEvent.class)` ) ;
312314 lines . push ( `@JsonSubTypes({` ) ;
313315 for ( let i = 0 ; i < variants . length ; i ++ ) {
314316 const v = variants [ i ] ;
@@ -378,20 +380,46 @@ async function generateUnknownEventClass(packageName: string, packageDir: string
378380 lines . push ( "" ) ;
379381 lines . push ( `package ${ packageName } ;` ) ;
380382 lines . push ( "" ) ;
383+ lines . push ( `import com.fasterxml.jackson.annotation.JsonIgnore;` ) ;
381384 lines . push ( `import com.fasterxml.jackson.annotation.JsonIgnoreProperties;` ) ;
385+ lines . push ( `import com.fasterxml.jackson.annotation.JsonProperty;` ) ;
382386 lines . push ( `import javax.annotation.processing.Generated;` ) ;
383387 lines . push ( "" ) ;
384388 lines . push ( `/**` ) ;
385389 lines . push ( ` * Fallback for event types not yet known to this SDK version.` ) ;
386390 lines . push ( ` *` ) ;
391+ lines . push ( ` * <p>The {@link #getOriginalType()} method returns the raw event-type discriminator` ) ;
392+ lines . push ( ` * value received on the wire, which can be used for forward-compatibility` ) ;
393+ lines . push ( ` * telemetry and handling.` ) ;
394+ lines . push ( ` *` ) ;
387395 lines . push ( ` * @since 1.0.0` ) ;
388396 lines . push ( ` */` ) ;
389397 lines . push ( `@JsonIgnoreProperties(ignoreUnknown = true)` ) ;
390398 lines . push ( GENERATED_ANNOTATION ) ;
391399 lines . push ( `public final class UnknownSessionEvent extends SessionEvent {` ) ;
392400 lines . push ( "" ) ;
401+ lines . push ( ` @JsonProperty("type")` ) ;
402+ lines . push ( ` private String originalType;` ) ;
403+ lines . push ( "" ) ;
404+ lines . push ( ` /**` ) ;
405+ lines . push ( ` * Returns the raw event-type discriminator string received on the wire,` ) ;
406+ lines . push ( ` * or {@code "unknown"} if the value was not present in the JSON payload.` ) ;
407+ lines . push ( ` *` ) ;
408+ lines . push ( ` * @return the original wire type string, or {@code "unknown"}` ) ;
409+ lines . push ( ` */` ) ;
393410 lines . push ( ` @Override` ) ;
394- lines . push ( ` public String getType() { return "unknown"; }` ) ;
411+ lines . push ( ` @JsonProperty("type")` ) ;
412+ lines . push ( ` public String getType() { return originalType != null ? originalType : "unknown"; }` ) ;
413+ lines . push ( "" ) ;
414+ lines . push ( ` /**` ) ;
415+ lines . push ( ` * Returns the raw event-type discriminator string received on the wire.` ) ;
416+ lines . push ( ` *` ) ;
417+ lines . push ( ` * @return the original wire type string, or {@code null} if not present` ) ;
418+ lines . push ( ` */` ) ;
419+ lines . push ( ` @JsonIgnore` ) ;
420+ lines . push ( ` public String getOriginalType() { return originalType; }` ) ;
421+ lines . push ( "" ) ;
422+ lines . push ( ` public void setOriginalType(String originalType) { this.originalType = originalType; }` ) ;
395423 lines . push ( `}` ) ;
396424 lines . push ( "" ) ;
397425
@@ -430,7 +458,6 @@ function renderNestedType(nested: JavaClassDef, indentLevel: number, nestedTypes
430458 lines . push ( `${ ind } }` ) ;
431459 } else if ( nested . kind === "class" && nested . schema ?. properties ) {
432460 const localNestedTypes = new Map < string , JavaClassDef > ( ) ;
433- const requiredSet = new Set ( nested . schema . required || [ ] ) ;
434461 const fields : { jsonName : string ; javaName : string ; javaType : string ; description ?: string } [ ] = [ ] ;
435462
436463 for ( const [ propName , propSchema ] of Object . entries ( nested . schema . properties ) ) {
0 commit comments