Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb6 committed Oct 3, 2024
1 parent 1d17cc0 commit 80b4008
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
23 changes: 15 additions & 8 deletions generators/php/codegen/src/context/PhpAttributeMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ export class PhpAttributeMapper {
);
}
if (underlyingInternalType.type === "union") {
const unionTypeParameters = this.getUnionTypeParameters(underlyingInternalType.types);
const unionTypeParameters = this.getUnionTypeParameters({
types: underlyingInternalType.types,
isOptional: type.isOptional()
});
// only add the attribute if deduping in getUnionTypeParameters resulted in more than one type
if (unionTypeParameters.length > 1) {
attributes.push(
php.attribute({
reference: this.context.getUnionClassReference(),
arguments: this.getUnionTypeParameters(underlyingInternalType.types)
arguments: unionTypeParameters
})
);
}
Expand All @@ -71,12 +74,16 @@ export class PhpAttributeMapper {
});
}

public getUnionTypeParameters(types: php.Type[]): php.AstNode[] {
public getUnionTypeParameters({
types,
isOptional = false
}: {
types: php.Type[];
isOptional?: boolean;
}): php.AstNode[] {
const typeAttributeArguments = types.map((type) => this.getTypeAttributeArgument(type));
// remove duplicates, such as "string" and "string" if enums and strings are both in the union
return uniqWith(
types.map((type) => this.getTypeAttributeArgument(type)),
isEqual
);
return uniqWith([...typeAttributeArguments, ...(isOptional ? [php.codeblock("'null'")] : [])], isEqual);
}

public getTypeAttributeArgument(type: php.Type): php.AstNode {
Expand Down Expand Up @@ -123,7 +130,7 @@ export class PhpAttributeMapper {
});
}
case "union": {
const unionTypeParameters = this.getUnionTypeParameters(type.internalType.types);
const unionTypeParameters = this.getUnionTypeParameters({ types: type.internalType.types });
if (unionTypeParameters.length === 1) {
if (unionTypeParameters[0] == null) {
throw new Error("Unexpected empty union type parameters");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,10 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator {
arguments_: UnnamedArgument[];
types: php.Type[];
}): php.CodeBlock {
const unionTypeParameters = this.context.phpAttributeMapper.getUnionTypeParameters(types);
const unionTypeParameters = this.context.phpAttributeMapper.getUnionTypeParameters({
types,
isOptional: false
});
// if deduping in getUnionTypeParameters results in one type, treat it like just that type
if (unionTypeParameters.length === 1) {
return this.decodeJsonResponse(types[0]);
Expand Down
2 changes: 1 addition & 1 deletion generators/php/sdk/src/endpoint/request/EndpointRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export abstract class EndpointRequest {
types: php.Type[];
isOptional: boolean;
}): php.CodeBlock {
const unionTypeParameters = this.context.phpAttributeMapper.getUnionTypeParameters(types);
const unionTypeParameters = this.context.phpAttributeMapper.getUnionTypeParameters({ types, isOptional });
// if deduping in getUnionTypeParameters results in one type, treat it like just that type
if (unionTypeParameters.length === 1) {
if (types[0] == null) {
Expand Down
2 changes: 0 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ service:


types:
TypeWithOptionalUnion:
properties:
myUnion: optional<MyUnion>

MyUnion:
docs: |
Several different types are accepted.
Expand Down

0 comments on commit 80b4008

Please sign in to comment.