Skip to content

Commit

Permalink
fix: fixes union type with discriminator in go
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethaasan committed Apr 25, 2024
1 parent b87ddc8 commit cb75748
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
12 changes: 4 additions & 8 deletions docs/migrations/version-3-to-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -264,19 +264,15 @@ type Car struct {
AdditionalProperties map[string]interface{}
}

func (serdp Car) IsVehicleType() bool {
return true
}
func (r Car) IsVehicleType() {}

type Truck struct {
VehicleType *VehicleType
RegistrationPlate string
AdditionalProperties map[string]interface{}
}

func (serdp Truck) IsVehicleType() bool {
return true
}
func (r Truck) IsVehicleType() {}

type VehicleType uint

Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 11 additions & 6 deletions src/generators/go/renderers/StructRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -71,7 +72,13 @@ export const GO_DEFAULT_STRUCT_PRESET: StructPresetType<GoOptions> = {
},
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}`;
Expand All @@ -81,10 +88,8 @@ export const GO_DEFAULT_STRUCT_PRESET: StructPresetType<GoOptions> = {
return '';
}

return `func (serdp ${model.name}) Is${FormatHelpers.toPascalCase(
return `func (r ${model.name}) Is${FormatHelpers.toPascalCase(
model.options.discriminator.discriminator
)}() bool {
return true
}`;
)}() {}`;
}
};
22 changes: 11 additions & 11 deletions src/generators/go/renderers/UnionRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
};

Expand All @@ -29,10 +32,7 @@ export class UnionRenderer extends GoRenderer<ConstrainedUnionModel> {
`${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}
Expand Down Expand Up @@ -99,6 +99,6 @@ export const GO_DEFAULT_UNION_PRESET: UnionPresetType<GoOptions> = {

return `Is${FormatHelpers.toPascalCase(
model.options.discriminator.discriminator
)}() bool`;
)}()`;
}
};
12 changes: 4 additions & 8 deletions test/generators/go/__snapshots__/GoGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -301,9 +299,7 @@ type Truck struct {
AdditionalProperties map[string]interface{}
}
func (serdp Truck) IsVehicleType() bool {
return true
}
func (r Truck) IsVehicleType() {}
",
]
`;
Expand Down

0 comments on commit cb75748

Please sign in to comment.