Skip to content

types: accept single-number ulimits when unmarshalling JSON - #988

Closed
Sanjays2402 wants to merge 1 commit into
compose-spec:mainfrom
Sanjays2402:980-ulimits-json
Closed

Sanjays2402 wants to merge 1 commit into
compose-spec:mainfrom
Sanjays2402:980-ulimits-json

Conversation

@Sanjays2402

Copy link
Copy Markdown

Fixes #980

UlimitsConfig.MarshalJSON writes a single limit as a bare number ("nproc": 65535), but there was no UnmarshalJSON to match, so unmarshalling a marshalled project with encoding/json failed:

json: cannot unmarshal number into Go struct field Project.services.app.ulimits.nproc of type types.UlimitsConfig

This adds UnmarshalJSON accepting both shapes MarshalJSON produces: a bare number for a single limit, and an object with soft and hard limits. A JSON null is left alone.

Repro, before the fix on main at 6ac9323: load a service with ulimits nproc 65535, json.Marshal the project, then json.Unmarshal it back. It failed with the error above; after the fix it round-trips, and the soft/hard form is unchanged.

Tests:

  • New TestUlimitsJSONRoundTrip in loader/tests/ulimits_test.go, covering both the single-number and soft/hard forms through a full project JSON round-trip.
  • go test ./... is green (types, loader, loader/tests and the rest of the tree).

UlimitsConfig.MarshalJSON writes a single limit as a bare number, but
there was no UnmarshalJSON, so unmarshalling a marshalled project with
encoding/json failed on that ulimit. Add UnmarshalJSON accepting both
shapes MarshalJSON produces: a bare number for a single limit, and an
object with soft and hard limits.

Fixes compose-spec#980

Signed-off-by: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com>
@ndeloof

ndeloof commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

we have tests in place to confirm marshalling / unmarshalling works as expected (see usage of roundTrip in loader/tests/), but unmarshalling relies on LoadWithContext, not on raw encoding/json.Unmarshal. This is the expected way: loader manages such multi-syntax support, and we have not plan to replicate the many corner cases with UnmarshalJSON functions on types

@ndeloof ndeloof closed this Nov 9, 2026
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

Successfully merging this pull request may close these issues.

Tools that pass a compose project as JSON fail when a service has ulimits: nproc: 65535

2 participants