Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Switch UOM API to YAML format #4462

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/core/metadata/controller/http/uom.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (C) 2022 IOTech Ltd
// Copyright (C) 2022-2023 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -38,8 +38,8 @@ func (uc *UnitOfMeasureController) UnitsOfMeasure(w http.ResponseWriter, r *http
utils.WriteHttpHeader(w, ctx, http.StatusOK)

switch r.Header.Get(common.Accept) {
case common.ContentTypeTOML:
pkg.EncodeAndWriteTomlResponse(u, w, lc)
case common.ContentTypeYAML:
pkg.EncodeAndWriteYamlResponse(u, w, lc)
default:
pkg.EncodeAndWriteResponse(response, w, lc)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/core/metadata/controller/http/uom_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//
// Copyright (C) 2022 IOTech Ltd
// Copyright (C) 2022-2023 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package http

import (
"encoding/json"
"gopkg.in/yaml.v3"
"net/http"
"net/http/httptest"
"testing"

"github.com/edgexfoundry/go-mod-bootstrap/v3/di"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/responses"
"github.com/pelletier/go-toml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -48,7 +48,7 @@ func TestUnitOfMeasureController_UnitsOfMeasure(t *testing.T) {
expectedResponse any
}{
{"valid - json response", common.ContentTypeJSON, responses.NewUnitsOfMeasureResponse("", "", http.StatusOK, testUoM)},
{"valid - toml response", common.ContentTypeTOML, testUoM},
{"valid - yaml response", common.ContentTypeYAML, testUoM},
}

for _, testCase := range tests {
Expand Down Expand Up @@ -76,10 +76,10 @@ func TestUnitOfMeasureController_UnitsOfMeasure(t *testing.T) {
assert.Empty(t, actualResponse.Message, "Message should be empty when it is successful")
} else {
actualResponse := uom.UnitsOfMeasureImpl{}
err = toml.Unmarshal(recorder.Body.Bytes(), &actualResponse)
err = yaml.Unmarshal(recorder.Body.Bytes(), &actualResponse)
require.NoError(t, err)

assert.Equal(t, actualResponse, testCase.expectedResponse, "TOML response not as expected")
assert.Equal(t, actualResponse, testCase.expectedResponse, "YAML response not as expected")
}
})
}
Expand Down
9 changes: 5 additions & 4 deletions internal/pkg/encoding.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright 2019 VMware Inc.
* Copyright (C) 2023 IOTech Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -15,11 +16,11 @@ package pkg

import (
"encoding/json"
"gopkg.in/yaml.v3"
"net/http"

"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
"github.com/pelletier/go-toml"
)

func EncodeAndWriteResponse(i interface{}, w http.ResponseWriter, LoggingClient logger.LoggingClient) {
Expand All @@ -35,10 +36,10 @@ func EncodeAndWriteResponse(i interface{}, w http.ResponseWriter, LoggingClient
}
}

func EncodeAndWriteTomlResponse(i interface{}, w http.ResponseWriter, lc logger.LoggingClient) {
w.Header().Set(common.ContentType, common.ContentTypeTOML)
func EncodeAndWriteYamlResponse(i interface{}, w http.ResponseWriter, lc logger.LoggingClient) {
w.Header().Set(common.ContentType, common.ContentTypeYAML)

enc := toml.NewEncoder(w)
enc := yaml.NewEncoder(w)
err := enc.Encode(i)
// Problems encoding
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion openapi/v3/core-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3808,7 +3808,7 @@ paths:
examples:
UnitsOfMeasureResponse:
$ref: '#/components/examples/UnitsOfMeasureResponseExample'
application/toml:
application/yaml:
schema:
$ref: '#/components/schemas/UnitsOfMeasure'
'500':
Expand Down