-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only add a prefix to enum types if conflicting types have different v…
…alues (#157) * Only add a prefix to enum types if conflicting types have different values. * Add test * update
- Loading branch information
1 parent
26f400a
commit 9c4d314
Showing
5 changed files
with
131 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pkg | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestGenEnumType tests that schemas that have conflicting | ||
// enums are legal. This is because they can have the | ||
// same enum names but with different values as inline | ||
// enums instead of refs. | ||
func TestGenEnumType(t *testing.T) { | ||
mustReadTestOpenAPIDoc(t, filepath.Join("testdata", "prefix_enum_type_on_collision_openapi.yml")) | ||
|
||
openAPICtx := &OpenAPIContext{ | ||
Doc: *testOpenAPIDoc, | ||
Pkg: &testPulumiPkg, | ||
} | ||
|
||
csharpNamespaces := map[string]string{ | ||
"": "Provider", | ||
} | ||
|
||
_, _, err := openAPICtx.GatherResourcesFromAPI(csharpNamespaces) | ||
assert.Nil(t, err) | ||
|
||
resourceSpec, ok := testPulumiPkg.Resources["fake-package:resource/v2:SomeResource"] | ||
assert.Truef(t, ok, "Expected to find a resource called SomeResource: %v", testPulumiPkg.Resources) | ||
|
||
// Due to the ordering of the paths, the type that has an enum prop point to a ref | ||
// would be encountered first and therefore would not have any collision. | ||
assert.Equal(t, "#/types/fake-package:resource/v2:EnumProp", resourceSpec.InputProperties["enumProp"].Ref) | ||
|
||
resourceSpec2, ok := testPulumiPkg.Resources["fake-package:resource/v2:SomeOtherResource"] | ||
assert.Truef(t, ok, "Expected to find a resource called SomeOtherResource: %v", testPulumiPkg.Resources) | ||
|
||
// When the next operation is encountered, the enum_prop property having an inline | ||
// schema with different values should be renamed with the prefix of the resource. | ||
assert.Equal(t, "#/types/fake-package:resource/v2:SomeOtherResourceEnumProp", resourceSpec2.InputProperties["enumProp"].Ref) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: Fake API | ||
version: "2.0" | ||
servers: | ||
- url: https://api.fake.com | ||
description: production | ||
|
||
components: | ||
schemas: | ||
an_enum_type: | ||
type: string | ||
enum: | ||
- a | ||
- b | ||
- c | ||
a_string_prop: | ||
type: string | ||
request_object_type: | ||
type: object | ||
properties: | ||
simple_prop: | ||
$ref: "#/components/schemas/a_string_prop" | ||
# This prop has a ref to a shared enum type. | ||
enum_prop: | ||
$ref: "#/components/schemas/an_enum_type" | ||
request_object_type2: | ||
type: object | ||
properties: | ||
simple_prop: | ||
$ref: "#/components/schemas/a_string_prop" | ||
# Whereas this prop, also called `enum_prop` | ||
# has an inline schema def. | ||
enum_prop: | ||
type: string | ||
enum: | ||
- d | ||
- e | ||
- f | ||
response_object_type: | ||
type: object | ||
properties: | ||
another_prop: | ||
$ref: "#/components/schemas/a_string_prop" | ||
|
||
paths: | ||
/v2/resource/someResource: | ||
post: | ||
operationId: create_some_resource | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/request_object_type" | ||
responses: | ||
"200": | ||
description: The response will be a JSON object with a key called `action`. | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/response_object_type" | ||
|
||
/v2/resource/someOtherResource: | ||
post: | ||
operationId: create_some_other_resource | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/request_object_type2" | ||
responses: | ||
"200": | ||
description: The response will be a JSON object with a key called `action`. | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/response_object_type" |