Skip to content

fix: keep the optional modifier of scalar fields in Avro, GraphQL, Protobuf and Rust - #309

Open
NopeCharon wants to merge 1 commit into
accordproject:mainfrom
NopeCharon:nopecharon/i306/scalar-optional-fields
Open

NopeCharon wants to merge 1 commit into
accordproject:mainfrom
NopeCharon:nopecharon/i306/scalar-optional-fields

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

Closes #306

Optional properties with a scalar type (o SSN taxId optional) are now generated as optional in Avro, GraphQL, Protobuf and Rust. Before this change they were generated as required.

Changes

  • In visit(), pass thing.isOptional() with the field returned by getScalarField(). That field is built from the scalar declaration and does not carry the property's optional modifier.
  • visitField takes an optional isOptional argument that defaults to field.isOptional(), and uses it everywhere it read field.isOptional() before. In Protobuf, concertoToProto3FieldRule takes the same optional argument.
  • Add a test per visitor with a model that has required, optional, optional DateTime and optional array scalar properties.
  • Update org.accordproject.protocol.v1_0_0-expected.proto: responseType, stateType, state, styles and result are optional in apapProtocol.cto, but the expected output had them as required.

Flags

  • This follows fix(typescript): propagate optional modifier for scalar fields #195, which fixed the same bug for TypeScript. CSharpVisitor.writeField also passes the property's optional modifier explicitly. Java and Go don't express optionality, and OData's Nullable already defaults to true, so those visitors are unchanged.
  • Each new test fails on main.

Screenshots or Video

N/A

Related Issues

Author Checklist

  • Ensure you provide a DCO sign-off for your commits using the --signoff option of git commit.
  • Vital features and changes captured in unit and/or integration tests
  • Commits messages follow AP format
  • Extend the documentation, if necessary
  • Merging to main from fork:branchname

…otobuf and Rust

These visitors dispatch fields with a scalar type to
visitField(thing.getScalarField(), parameters). The field built by
getScalarField() comes from the scalar declaration. It keeps the
property name and array flag but not the optional modifier, so
`o SSN taxId optional` was generated as a required field:

- Avro: `string taxId;` instead of `union { null, string } taxId;`
- GraphQL: `taxId: String!` instead of `taxId: String`
- Protobuf: `string taxId` instead of `optional string taxId`
- Rust: `String` instead of `Option<String>`, so valid JSON without the
  property failed to deserialize

Pass thing.isOptional() into visitField as an optional isOptional
argument that defaults to field.isOptional(), and use it in place of
field.isOptional() in visitField and, for Protobuf, in
concertoToProto3FieldRule. TypeScript had the same bug (accordproject#194), and
CSharpVisitor already passes the property's optional modifier
explicitly.

The expected Protobuf output for the APAP protocol model is updated:
responseType, stateType, state, styles and result are optional in
apapProtocol.cto.

Closes accordproject#306

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
@mira-accord-project

Copy link
Copy Markdown

Thanks for your contribution, and for opening #306 with the details alongside this PR. The CI workflows need a maintainer's approval because this is a first contribution; that has been requested and the checks should start shortly. After that, the PR waits for review from the Concerto maintainers. If you push more changes, the checks will run again, and it helps if the commits are signed off (DCO).

Drafted by an Accord Project agent, reviewed by a maintainer.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: optional properties with a scalar type are generated as required in Avro, GraphQL, Protobuf and Rust

1 participant