From cb75748c54689986048a89f4c8b4b0ce64903606 Mon Sep 17 00:00:00 2001 From: Kenneth Aasan Date: Thu, 25 Apr 2024 14:03:55 +0200 Subject: [PATCH] fix: fixes union type with discriminator in go --- docs/migrations/version-3-to-4.md | 12 ++++------ src/generators/go/renderers/StructRenderer.ts | 17 +++++++++----- src/generators/go/renderers/UnionRenderer.ts | 22 +++++++++---------- .../go/__snapshots__/GoGenerator.spec.ts.snap | 12 ++++------ 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/docs/migrations/version-3-to-4.md b/docs/migrations/version-3-to-4.md index cdc8ec5008..d5355f334c 100644 --- a/docs/migrations/version-3-to-4.md +++ b/docs/migrations/version-3-to-4.md @@ -255,7 +255,7 @@ While the above changes work for primitives, it's problematic for objects with a ```go type Vehicle interface { - IsVehicleType() bool + IsVehicleType() } type Car struct { @@ -264,9 +264,7 @@ type Car struct { AdditionalProperties map[string]interface{} } -func (serdp Car) IsVehicleType() bool { - return true -} +func (r Car) IsVehicleType() {} type Truck struct { VehicleType *VehicleType @@ -274,9 +272,7 @@ type Truck struct { AdditionalProperties map[string]interface{} } -func (serdp Truck) IsVehicleType() bool { - return true -} +func (r Truck) IsVehicleType() {} type VehicleType uint @@ -307,7 +303,7 @@ Modelina now has support for nullable and required properties in go structs. Thi ```go type info struct { name string // required - description *string // nullable + description *string // nullable version *float64 isDevelopment *bool } diff --git a/src/generators/go/renderers/StructRenderer.ts b/src/generators/go/renderers/StructRenderer.ts index e2bfa9e243..4efd70fcc0 100644 --- a/src/generators/go/renderers/StructRenderer.ts +++ b/src/generators/go/renderers/StructRenderer.ts @@ -3,7 +3,8 @@ import { StructPresetType } from '../GoPreset'; import { ConstrainedObjectModel, ConstrainedObjectPropertyModel, - ConstrainedReferenceModel + ConstrainedReferenceModel, + ConstrainedUnionModel } from '../../../models'; import { GoOptions } from '../GoGenerator'; import { FormatHelpers } from '../../../helpers/FormatHelpers'; @@ -71,7 +72,13 @@ export const GO_DEFAULT_STRUCT_PRESET: StructPresetType = { }, field({ field }) { let fieldType = field.property.type; - if (field.property instanceof ConstrainedReferenceModel) { + if ( + field.property instanceof ConstrainedReferenceModel && + !( + field.property.ref instanceof ConstrainedUnionModel && + field.property.ref.options.discriminator + ) + ) { fieldType = `*${fieldType}`; } return `${field.propertyName} ${fieldType}`; @@ -81,10 +88,8 @@ export const GO_DEFAULT_STRUCT_PRESET: StructPresetType = { return ''; } - return `func (serdp ${model.name}) Is${FormatHelpers.toPascalCase( + return `func (r ${model.name}) Is${FormatHelpers.toPascalCase( model.options.discriminator.discriminator - )}() bool { - return true -}`; + )}() {}`; } }; diff --git a/src/generators/go/renderers/UnionRenderer.ts b/src/generators/go/renderers/UnionRenderer.ts index 0caa90f967..4e38b0ac86 100644 --- a/src/generators/go/renderers/UnionRenderer.ts +++ b/src/generators/go/renderers/UnionRenderer.ts @@ -9,12 +9,15 @@ import { import { GoOptions } from '../GoGenerator'; import { FormatHelpers } from '../../../helpers/FormatHelpers'; -const unionIncludesPrimitives = (model: ConstrainedUnionModel): boolean => { - return !model.union.every( - (union) => - union instanceof ConstrainedObjectModel || - (union instanceof ConstrainedReferenceModel && - union.ref instanceof ConstrainedObjectModel) +const unionIncludesDiscriminator = (model: ConstrainedUnionModel): boolean => { + return ( + !!model.options.discriminator && + model.union.every( + (union) => + union instanceof ConstrainedObjectModel || + (union instanceof ConstrainedReferenceModel && + union.ref instanceof ConstrainedObjectModel) + ) ); }; @@ -29,10 +32,7 @@ export class UnionRenderer extends GoRenderer { `${this.model.name} represents a ${this.model.name} model.` ); - if ( - !unionIncludesPrimitives(this.model) && - this.model.options.discriminator - ) { + if (unionIncludesDiscriminator(this.model)) { const content: string[] = [await this.runDiscriminatorAccessorPreset()]; return `${doc} @@ -99,6 +99,6 @@ export const GO_DEFAULT_UNION_PRESET: UnionPresetType = { return `Is${FormatHelpers.toPascalCase( model.options.discriminator.discriminator - )}() bool`; + )}()`; } }; diff --git a/test/generators/go/__snapshots__/GoGenerator.spec.ts.snap b/test/generators/go/__snapshots__/GoGenerator.spec.ts.snap index 76e38abc92..a2152d1d9d 100644 --- a/test/generators/go/__snapshots__/GoGenerator.spec.ts.snap +++ b/test/generators/go/__snapshots__/GoGenerator.spec.ts.snap @@ -254,11 +254,11 @@ exports[`GoGenerator should render interfaces for objects with discriminator 1`] Array [ "// Vehicle represents a Vehicle model. type Vehicle interface { - IsVehicleType() bool + IsVehicleType() }", "// Cargo represents a Cargo model. type Cargo struct { - Vehicle *Vehicle + Vehicle Vehicle AdditionalProperties map[string]interface{} }", "// Car represents a Car model. @@ -268,9 +268,7 @@ type Car struct { AdditionalProperties map[string]interface{} } -func (serdp Car) IsVehicleType() bool { - return true -} +func (r Car) IsVehicleType() {} ", "// VehicleType represents an enum of VehicleType. type VehicleType uint @@ -301,9 +299,7 @@ type Truck struct { AdditionalProperties map[string]interface{} } -func (serdp Truck) IsVehicleType() bool { - return true -} +func (r Truck) IsVehicleType() {} ", ] `;