Skip to content

Commit

Permalink
fix: added avro error reason to message header (#41) 4b191b2 @Simun17
Browse files Browse the repository at this point in the history
…feat: default header schema (#39) 8d51574 @Simun17 refactor: change message when empty (#44) ef8f07f @Simun17 fix: require schema format (#45)
  • Loading branch information
Simun17 authored Jan 13, 2025
1 parent 00b415f commit 2320978
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions registry/registry/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
var ErrNotFound = errors.New("not found")
var ErrUnknownComp = errors.New("unknown value for compatibility_mode")
var ErrUnknownVal = errors.New("unknown value for validity mode")
var ErrUnknownFormat = errors.New("unknown value for schema format")
var ErrNotValid = errors.New("schema is not valid")
var ErrNotComp = errors.New("schemas are not compatible")
var ErrInvalidValueHeader = errors.New("invalid header value")
Expand Down
22 changes: 16 additions & 6 deletions registry/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,8 @@ func (h Handler) GetSpecificationByIdAndVersion(w http.ResponseWriter, r *http.R
log.Println(err)
}

body, _ := json.Marshal(specification)
writeResponse(w, responseBodyAndCode{
Body: body,
Body: specification,
Code: http.StatusOK,
})
}
Expand Down Expand Up @@ -359,8 +358,8 @@ func (h Handler) GetSchemas(w http.ResponseWriter, _ *http.Request) {
if err != nil {
if errors.Is(err, registry.ErrNotFound) {
writeResponse(w, responseBodyAndCode{
Body: serializeErrorMessage(http.StatusText(http.StatusNotFound)),
Code: http.StatusNotFound,
Body: serializeErrorMessage("No active schemas registered in the Registry"),
Code: http.StatusNoContent,
})
return
}
Expand Down Expand Up @@ -535,8 +534,19 @@ func (h Handler) SearchSchemas(w http.ResponseWriter, r *http.Request) {
// @Failure 500
// @Router /schemas [post]
func (h Handler) PostSchema(w http.ResponseWriter, r *http.Request) {

registerRequest, err := readSchemaRegisterRequest(r.Body)
if err != nil {
if errors.Is(err, registry.ErrUnknownFormat) {
body, _ := json.Marshal(report{
Message: "Bad request: unknown format value",
})
writeResponse(w, responseBodyAndCode{
Body: body,
Code: http.StatusBadRequest,
})
return
}
writeResponse(w, responseBodyAndCode{
Body: serializeErrorMessage(http.StatusText(http.StatusBadRequest)),
Code: http.StatusBadRequest,
Expand All @@ -548,7 +558,7 @@ func (h Handler) PostSchema(w http.ResponseWriter, r *http.Request) {
if err != nil {
if errors.Is(err, registry.ErrUnknownComp) {
body, _ := json.Marshal(report{
Message: "Bad request: unknown value for compatibility_mode",
Message: "Bad request: unknown compatibility_mode value",
})
writeResponse(w, responseBodyAndCode{
Body: body,
Expand All @@ -559,7 +569,7 @@ func (h Handler) PostSchema(w http.ResponseWriter, r *http.Request) {

if errors.Is(err, registry.ErrUnknownVal) {
body, _ := json.Marshal(report{
Message: "Bad request: unknown value for validity_mode",
Message: "Bad request: unknown validity_mode value",
})
writeResponse(w, responseBodyAndCode{
Body: body,
Expand Down
18 changes: 18 additions & 0 deletions registry/server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"io"
"net/http"
"strings"

"github.com/dataphos/schema-registry/registry"
)
Expand All @@ -27,6 +28,8 @@ type responseBodyAndCode struct {
Code int
}

var supportedFormats = []string{"json", "avro", "xml", "csv", "protobuf"}

func writeResponse(w http.ResponseWriter, response responseBodyAndCode) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(response.Code)
Expand All @@ -49,9 +52,24 @@ func readSchemaRegisterRequest(body io.ReadCloser) (registry.SchemaRegistrationR
return registry.SchemaRegistrationRequest{}, err
}

// check if format is unknown
format := strings.ToLower(schemaRegisterRequest.SchemaType)
if !containsFormat(format) {
return registry.SchemaRegistrationRequest{}, registry.ErrUnknownFormat
}

return schemaRegisterRequest, nil
}

func containsFormat(format string) bool {
for _, supportedFormat := range supportedFormats {
if format == supportedFormat {
return true
}
}
return false
}

func readSchemaUpdateRequest(body io.ReadCloser) (registry.SchemaUpdateRequest, error) {
encoded, err := io.ReadAll(body)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static boolean checkValidity(String schemaType, String schema, String mod
case "full" -> ValidityLevel.FULL;
default -> ValidityLevel.NONE;
};
ContentValidator validator = ValidatorFactory.createValidator(schemaType);
ContentValidator validator = ValidatorFactory.createValidator(schemaType.toLowerCase());
if (validator == null) { // in case ValidatorFactory returns null
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion registry/validity/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func CheckOverHTTP(ctx context.Context, schemaType, schema, mode, url string) (b

var parsedBody checkResponse
if err = json.Unmarshal(body, &parsedBody); err != nil {
return false, "error unmarshalling compatibility check body", err
return false, "error unmarshalling validity check body", err
}

valid := parsedBody.Result
Expand Down

0 comments on commit 2320978

Please sign in to comment.