-
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.
Treat oneOf without discriminator in resource inputs as allOf (#160)
* Treat oneOf without discriminator in resource inputs as allOf * Update enum collisions test * Fix lint error * Add test for resource oneOf without discriminator
- Loading branch information
1 parent
9c4d314
commit c1a3f65
Showing
5 changed files
with
193 additions
and
34 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,41 @@ | ||
package pkg | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestResourceInputsWithOneOfNoDiscriminator tests if | ||
// resource request bodies with oneOf definitions that | ||
// don't define a discriminator are treated as allOf | ||
// with none of the inputs being required. | ||
func TestResourceInputsWithOneOfNoDiscriminator(t *testing.T) { | ||
mustReadTestOpenAPIDoc(t, filepath.Join("testdata", "resource_inputs_oneof_no_discriminator_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:Resource"] | ||
assert.Truef(t, ok, "Expected to find a resource called Resource: %v", testPulumiPkg.Resources) | ||
|
||
// The resource should have all props from all types that was in the oneOf | ||
// definition. | ||
assert.Equal(t, "string", resourceSpec.InputProperties["simpleProp"].Type) | ||
assert.Equal(t, "string", resourceSpec.InputProperties["anotherProp"].Type) | ||
// None of them should be required because the API does not | ||
// describe a discriminator. It's up to the user to know | ||
// which set of inputs are required based on the "type" | ||
// of request they would like to send to the API. | ||
assert.Empty(t, resourceSpec.Required) | ||
} |
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
54 changes: 54 additions & 0 deletions
54
pkg/testdata/resource_inputs_oneof_no_discriminator_openapi.yml
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,54 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: Fake API | ||
version: "2.0" | ||
servers: | ||
- url: https://api.fake.com | ||
description: production | ||
|
||
components: | ||
schemas: | ||
a_string_prop: | ||
type: string | ||
|
||
request_object_type: | ||
type: object | ||
properties: | ||
simple_prop: | ||
$ref: "#/components/schemas/a_string_prop" | ||
required: | ||
- simple_prop | ||
|
||
request_object_type2: | ||
type: object | ||
properties: | ||
another_prop: | ||
$ref: "#/components/schemas/a_string_prop" | ||
required: | ||
- another_prop | ||
|
||
responses: | ||
response_object_type: | ||
description: The response will be a JSON object with a key called `action`. | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
another_prop: | ||
$ref: "#/components/schemas/a_string_prop" | ||
|
||
paths: | ||
/v2/resource: | ||
post: | ||
operationId: create_resource | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
oneOf: | ||
- $ref: "#/components/schemas/request_object_type" | ||
- $ref: "#/components/schemas/request_object_type2" | ||
responses: | ||
"200": | ||
$ref: "#/components/responses/response_object_type" |