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

Proposal: Ability to JSON errors with private fields #14

Closed
PROger4ever opened this issue Jun 1, 2018 · 0 comments
Closed

Proposal: Ability to JSON errors with private fields #14

PROger4ever opened this issue Jun 1, 2018 · 0 comments

Comments

@PROger4ever
Copy link

PROger4ever commented Jun 1, 2018

Hello.
It would be very useful to have a builtin way to Marshal and Unmarshal validation errors completely (including fields "code" and "message"). This feature will allow us to send validation errors directly to API clients without wrapping them, etc.
I see the following 2 ways to implement it:

  1. Make "code" and "message" public. This makes uncontrollable changes to be possible.
  2. Add custom marshaling/unmarshaling to error structs like this:
func (e *Validation) MarshalJSON() ([]byte, error) {
	return json.Marshal(map[string]interface{}{
		"code": e.code,
		"name": e.Name,
		"in": e.In,
		"value": e.Value,
		"message": e.message,
		"values": e.Values,
	})
}

func (e *Validation) UnmarshalJSON(data []byte) error {
	theMap := make(map[string]interface{})
	err := json.Unmarshal(data, theMap)
	if err != nil {
		return err
	}

	e.code = theMap["code"].(int32)
	e.Name = theMap["name"].(string)
	e.In = theMap["in"].(string)
	e.Value = theMap["value"]
	e.message = theMap["message"].(string)
	e.Values = theMap["values"].([]interface{})
	return nil
}

Could you implement this feature, please?
Thank you in advance.

@PROger4ever PROger4ever changed the title Ability to JSON errors with private fields Proposal: Ability to JSON errors with private fields Jun 1, 2018
fredbi added a commit to fredbi/errors that referenced this issue Feb 7, 2021
* fixes go-openapi#14
* updated: linters config, README.md (link to go.pk.dev for doc)

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi fredbi closed this as completed in c694523 Feb 7, 2021
fredbi added a commit to fredbi/go-swagger that referenced this issue Feb 7, 2021
* fixes go-swagger#2428 (see also go-openapi/spec#97)

* re-enacted appveyor CI to assert go-swagger runs on windows
* bring in the following fixes from the go-openapi packages:
  * fixes go-swagger#2506 [spec marshalling as JSON no more exhibits an empty AuthorizationURL when flow is not accessCode or implicit]
  * flatten can operate on remote URLs, even when there are circular $ref's (go-openapi/analysis#57)
  * flatten can operate without BasePath (assume current working directory) (go-openapi/analysis#66)
  * swag spec LoadFromFileOrHTTP() can load file URIs on windows, including UNCs (shared volumes) (relates to go-openapi/spec#97)
  * errors support JSON marshalling (enhancement: go-openapi/errors#14)

* test: removed assertions on logs which may fail during parallel testing

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to go-swagger/go-swagger that referenced this issue Feb 8, 2021
* windows support: updated go-openapi dependencies

* fixes #2428 (see also go-openapi/spec#97)

* re-enacted appveyor CI to assert go-swagger runs on windows
* bring in the following fixes from the go-openapi packages:
  * fixes #2506 [spec marshalling as JSON no more exhibits an empty AuthorizationURL when flow is not accessCode or implicit]
  * flatten can operate on remote URLs, even when there are circular $ref's (go-openapi/analysis#57)
  * flatten can operate without BasePath (assume current working directory) (go-openapi/analysis#66)
  * swag spec LoadFromFileOrHTTP() can load file URIs on windows, including UNCs (shared volumes) (relates to go-openapi/spec#97)
  * errors support JSON marshalling (enhancement: go-openapi/errors#14)

* test: removed assertions on logs which may fail during parallel testing

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

* rebalanced specs used for codegen between CI jobs

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants