Skip to content

Commit

Permalink
Split the synthetics request port field into a oneOf (#2678)
Browse files Browse the repository at this point in the history
* update formatter to stop matching templated strings in int schemas

* Regenerate client from commit 3288a6f0 of spec repo

---------

Co-authored-by: Kevin Zou <kevin.zou@datadoghq.com>
Co-authored-by: ci.datadog-api-spec <packages@datadoghq.com>
  • Loading branch information
3 people authored Sep 11, 2024
1 parent d118537 commit 92128b1
Show file tree
Hide file tree
Showing 28 changed files with 220 additions and 91 deletions.
8 changes: 4 additions & 4 deletions .apigentools-info
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"spec_versions": {
"v1": {
"apigentools_version": "1.6.6",
"regenerated": "2024-09-10 15:40:05.864991",
"spec_repo_commit": "80dc4248"
"regenerated": "2024-09-11 19:15:27.997097",
"spec_repo_commit": "3288a6f0"
},
"v2": {
"apigentools_version": "1.6.6",
"regenerated": "2024-09-10 15:40:05.883051",
"spec_repo_commit": "80dc4248"
"regenerated": "2024-09-11 19:15:28.014917",
"spec_repo_commit": "3288a6f0"
}
}
}
16 changes: 14 additions & 2 deletions .generator/schemas/v1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16352,8 +16352,7 @@ components:
description: Persist cookies across redirects.
type: boolean
port:
description: Port to use when performing the test.
type: string
$ref: '#/components/schemas/SyntheticsTestRequestPort'
proxy:
$ref: '#/components/schemas/SyntheticsTestRequestProxy'
query:
Expand Down Expand Up @@ -16457,6 +16456,15 @@ components:
description: Date of update of the certificate or key, ISO format.
type: string
type: object
SyntheticsTestRequestNumericalPort:
description: Integer Port number to use when performing the test.
format: int64
type: integer
SyntheticsTestRequestPort:
description: Port to use when performing the test.
oneOf:
- $ref: '#/components/schemas/SyntheticsTestRequestNumericalPort'
- $ref: '#/components/schemas/SyntheticsTestRequestVariablePort'
SyntheticsTestRequestProxy:
description: The proxy to perform the test.
properties:
Expand All @@ -16469,6 +16477,10 @@ components:
required:
- url
type: object
SyntheticsTestRequestVariablePort:
description: String Port number to use when performing the test. Supports templated
variables.
type: string
SyntheticsTiming:
description: 'Object containing all metrics and their values collected for a
Synthetic API test.
Expand Down
13 changes: 10 additions & 3 deletions .generator/src/generator/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,19 +448,26 @@ def format_bool(x):
raise TypeError(f"{x} is not supported type {schema}")
return "true" if x else "false"

def format_int(x):
r = str(x)
if "{{" in r and "}}" in r:
# workaround to stop matching templated strings
raise TypeError(f"{x} is not supported type {schema}")
return r

def format_uuid(x):
return f'uuid.MustParse("{x}")'

def open_file(x):
return f"func() io.Reader {{ fp, _ := os.Open({format_string(x)}); return fp }}()"

formatters = {
"int32": str,
"int64": str,
"int32": format_int,
"int64": format_int,
"double": format_double,
"date-time": format_datetime,
"number": format_number,
"integer": str,
"integer": format_int,
"boolean": format_bool,
"string": format_string,
"email": format_string,
Expand Down
14 changes: 7 additions & 7 deletions api/datadogV1/model_synthetics_test_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type SyntheticsTestRequest struct {
// Persist cookies across redirects.
PersistCookies *bool `json:"persistCookies,omitempty"`
// Port to use when performing the test.
Port *string `json:"port,omitempty"`
Port *SyntheticsTestRequestPort `json:"port,omitempty"`
// The proxy to perform the test.
Proxy *SyntheticsTestRequestProxy `json:"proxy,omitempty"`
// Query to use for the test.
Expand Down Expand Up @@ -711,17 +711,17 @@ func (o *SyntheticsTestRequest) SetPersistCookies(v bool) {
}

// GetPort returns the Port field value if set, zero value otherwise.
func (o *SyntheticsTestRequest) GetPort() string {
func (o *SyntheticsTestRequest) GetPort() SyntheticsTestRequestPort {
if o == nil || o.Port == nil {
var ret string
var ret SyntheticsTestRequestPort
return ret
}
return *o.Port
}

// GetPortOk returns a tuple with the Port field value if set, nil otherwise
// and a boolean to check if the value has been set.
func (o *SyntheticsTestRequest) GetPortOk() (*string, bool) {
func (o *SyntheticsTestRequest) GetPortOk() (*SyntheticsTestRequestPort, bool) {
if o == nil || o.Port == nil {
return nil, false
}
Expand All @@ -733,8 +733,8 @@ func (o *SyntheticsTestRequest) HasPort() bool {
return o != nil && o.Port != nil
}

// SetPort gets a reference to the given string and assigns it to the Port field.
func (o *SyntheticsTestRequest) SetPort(v string) {
// SetPort gets a reference to the given SyntheticsTestRequestPort and assigns it to the Port field.
func (o *SyntheticsTestRequest) SetPort(v SyntheticsTestRequestPort) {
o.Port = &v
}

Expand Down Expand Up @@ -1062,7 +1062,7 @@ func (o *SyntheticsTestRequest) UnmarshalJSON(bytes []byte) (err error) {
NoSavingResponseBody *bool `json:"noSavingResponseBody,omitempty"`
NumberOfPackets *int32 `json:"numberOfPackets,omitempty"`
PersistCookies *bool `json:"persistCookies,omitempty"`
Port *string `json:"port,omitempty"`
Port *SyntheticsTestRequestPort `json:"port,omitempty"`
Proxy *SyntheticsTestRequestProxy `json:"proxy,omitempty"`
Query interface{} `json:"query,omitempty"`
Servername *string `json:"servername,omitempty"`
Expand Down
105 changes: 105 additions & 0 deletions api/datadogV1/model_synthetics_test_request_port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

package datadogV1

import (
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)

// SyntheticsTestRequestPort - Port to use when performing the test.
type SyntheticsTestRequestPort struct {
SyntheticsTestRequestNumericalPort *int64
SyntheticsTestRequestVariablePort *string

// UnparsedObject contains the raw value of the object if there was an error when deserializing into the struct
UnparsedObject interface{}
}

// SyntheticsTestRequestNumericalPortAsSyntheticsTestRequestPort is a convenience function that returns int64 wrapped in SyntheticsTestRequestPort.
func SyntheticsTestRequestNumericalPortAsSyntheticsTestRequestPort(v *int64) SyntheticsTestRequestPort {
return SyntheticsTestRequestPort{SyntheticsTestRequestNumericalPort: v}
}

// SyntheticsTestRequestVariablePortAsSyntheticsTestRequestPort is a convenience function that returns string wrapped in SyntheticsTestRequestPort.
func SyntheticsTestRequestVariablePortAsSyntheticsTestRequestPort(v *string) SyntheticsTestRequestPort {
return SyntheticsTestRequestPort{SyntheticsTestRequestVariablePort: v}
}

// UnmarshalJSON turns data into one of the pointers in the struct.
func (obj *SyntheticsTestRequestPort) UnmarshalJSON(data []byte) error {
var err error
match := 0
// try to unmarshal data into SyntheticsTestRequestNumericalPort
err = datadog.Unmarshal(data, &obj.SyntheticsTestRequestNumericalPort)
if err == nil {
if obj.SyntheticsTestRequestNumericalPort != nil {
jsonSyntheticsTestRequestNumericalPort, _ := datadog.Marshal(obj.SyntheticsTestRequestNumericalPort)
if string(jsonSyntheticsTestRequestNumericalPort) == "{}" { // empty struct
obj.SyntheticsTestRequestNumericalPort = nil
} else {
match++
}
} else {
obj.SyntheticsTestRequestNumericalPort = nil
}
} else {
obj.SyntheticsTestRequestNumericalPort = nil
}

// try to unmarshal data into SyntheticsTestRequestVariablePort
err = datadog.Unmarshal(data, &obj.SyntheticsTestRequestVariablePort)
if err == nil {
if obj.SyntheticsTestRequestVariablePort != nil {
jsonSyntheticsTestRequestVariablePort, _ := datadog.Marshal(obj.SyntheticsTestRequestVariablePort)
if string(jsonSyntheticsTestRequestVariablePort) == "{}" { // empty struct
obj.SyntheticsTestRequestVariablePort = nil
} else {
match++
}
} else {
obj.SyntheticsTestRequestVariablePort = nil
}
} else {
obj.SyntheticsTestRequestVariablePort = nil
}

if match != 1 { // more than 1 match
// reset to nil
obj.SyntheticsTestRequestNumericalPort = nil
obj.SyntheticsTestRequestVariablePort = nil
return datadog.Unmarshal(data, &obj.UnparsedObject)
}
return nil // exactly one match
}

// MarshalJSON turns data from the first non-nil pointers in the struct to JSON.
func (obj SyntheticsTestRequestPort) MarshalJSON() ([]byte, error) {
if obj.SyntheticsTestRequestNumericalPort != nil {
return datadog.Marshal(&obj.SyntheticsTestRequestNumericalPort)
}

if obj.SyntheticsTestRequestVariablePort != nil {
return datadog.Marshal(&obj.SyntheticsTestRequestVariablePort)
}

if obj.UnparsedObject != nil {
return datadog.Marshal(obj.UnparsedObject)
}
return nil, nil // no data in oneOf schemas
}

// GetActualInstance returns the actual instance.
func (obj *SyntheticsTestRequestPort) GetActualInstance() interface{} {
if obj.SyntheticsTestRequestNumericalPort != nil {
return obj.SyntheticsTestRequestNumericalPort
}

if obj.SyntheticsTestRequestVariablePort != nil {
return obj.SyntheticsTestRequestVariablePort
}

// all schemas are nil
return nil
}
3 changes: 2 additions & 1 deletion examples/v1/synthetics/CreateSyntheticsAPITest_1072503741.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func main() {
},
Request: &datadogV1.SyntheticsTestRequest{
Host: datadog.PtrString("datadoghq.com"),
Port: datadog.PtrString("443"),
Port: &datadogV1.SyntheticsTestRequestPort{
SyntheticsTestRequestVariablePort: datadog.PtrString("{{ DATADOG_PORT }}")},
},
},
Locations: []string{
Expand Down
5 changes: 3 additions & 2 deletions examples/v1/synthetics/CreateSyntheticsAPITest_1279271422.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ func main() {
}},
},
Request: datadogV1.SyntheticsTestRequest{
Host: datadog.PtrString("grpcbin.test.k6.io"),
Port: datadog.PtrString("9000"),
Host: datadog.PtrString("grpcbin.test.k6.io"),
Port: &datadogV1.SyntheticsTestRequestPort{
SyntheticsTestRequestNumericalPort: datadog.PtrInt64(9000)},
Service: datadog.PtrString("grpcbin.GRPCBin"),
Method: datadog.PtrString("Index"),
Message: datadog.PtrString("{}"),
Expand Down
5 changes: 3 additions & 2 deletions examples/v1/synthetics/CreateSyntheticsAPITest_1402674167.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func main() {
}},
},
Request: &datadogV1.SyntheticsTestRequest{
Host: datadog.PtrString("localhost"),
Port: datadog.PtrString("50051"),
Host: datadog.PtrString("localhost"),
Port: &datadogV1.SyntheticsTestRequestPort{
SyntheticsTestRequestNumericalPort: datadog.PtrInt64(50051)},
Service: datadog.PtrString("Hello"),
Method: datadog.PtrString("GET"),
Message: datadog.PtrString(""),
Expand Down
3 changes: 2 additions & 1 deletion examples/v1/synthetics/CreateSyntheticsAPITest_3829801148.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func main() {
Request: &datadogV1.SyntheticsTestRequest{
Host: datadog.PtrString("https://datadoghq.com"),
Message: datadog.PtrString("message"),
Port: datadog.PtrString("443"),
Port: &datadogV1.SyntheticsTestRequestPort{
SyntheticsTestRequestNumericalPort: datadog.PtrInt64(443)},
},
},
Locations: []string{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-08-21T13:17:18.467Z
2024-09-10T20:52:06.237Z
Loading

0 comments on commit 92128b1

Please sign in to comment.