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

Reflected contract schema invalid for all optional arguments #154

Open
griffithsrac opened this issue Nov 18, 2024 · 0 comments
Open

Reflected contract schema invalid for all optional arguments #154

griffithsrac opened this issue Nov 18, 2024 · 0 comments

Comments

@griffithsrac
Copy link

Currently you cannot create a contract with a method in which all arguments are optional. Instead you get an error when creating the chaincode as the reflected metadata doesn't produce a valid JSON schema. This is because there are some components with an empty required field, whereas draft-04 of the JSON schema specifies all required fields must contain at least 1 value (so if all fields are optional the required field should be omitted entirely instead),

Example:

package main

import (
	"github.com/hyperledger/fabric-contract-api-go/v2/contractapi"
)

// SimpleContract with biz logic
type SimpleContract struct {
	contractapi.Contract
}

type Args struct {
	OptionalOne string `json:"optional_one" metadata:",optional"`
	OptionalTwo string `json:"optional_two" metadata:",optional"`
}

// HelloWorld - returns a string
func (sc *SimpleContract) HelloWorld(ctx contractapi.TransactionContextInterface, args *Args) string {
	return "Hello World"
}

func main() {
	simpleContract := new(SimpleContract)

	cc, err := contractapi.NewChaincode(simpleContract)

	if err != nil {
		panic(err.Error())
	}

	if err := cc.Start(); err != nil {
		panic(err.Error())
	}
}

Will give:

panic: cannot use metadata. Metadata did not match schema:
1. components.schemas.Args.required: Array must have at least 1 items

I think the issue here is that:

  • the contract schema is generated through reflection and includes an empty array ("required:" []) i.e. all fields are optional.
  • the version of JSON schema used via packr (see metadata/schema/schema.json) has "objecttype.properties.required.$ref": "http://json-schema.org/draft-04/schema#/definitions/stringArray"
  • and in draft-04 stringArray's had to have a minimum length of 1.

I think this can probably be fixed in a number of ways...

  1. We could add omitempty to the JSON tag of the required field in metadata.ObjectMetadata so that if there are no required fields then the required field isn't included (to match draft-04 of the spec). I think this should just be a 1 word fix and mean that the generated contract schema is correct as per draft-04 schemas.
  2. Or we change which draft is used to a later version (which allows for empty required lists), i.e. explicitly saying all fields are optional. Not sure what other changes have been made since draft-04 though, so I can't say if this would have other effects or not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant