10BC0 api/types/network: generate network-inspect struct definitions from Swagger spec by corhere · Pull Request #50855 · moby/moby · GitHub
[go: up one dir, main page]

Skip to content

Conversation

corhere
Copy link
Contributor
@corhere corhere commented Aug 29, 2025

- What I did

  • Replaced the hand-written struct definitions of network.Inspect, network.Summary and some of the referenced struct definitions with functionally-equivalent ones generated from the Swagger spec.
  • Added some missing network definitions to the Swagger spec.

- How I did it
I had to fork another go-swagger template to work around a curious behaviour where go-swagger would always force the "v" in IPv4 or IPv6 to uppercase. It's not supposed to do that.

Convincing go-swagger to generate struct definitions with non-pointer struct-valued fields is not too difficult once you get the hang of it. The trick is annotating the definition of the referenced type with x-omitempty: false and x-nullable: false. Passing --keep-spec-order allows us to maintain the exact field order as the hand-rolled definitions to make them 100% drop-in replacements.

- How to verify it
By inspection. The new structs should be identical to the hand-written structs aside from doc comment formatting and the presence of json struct-tags that explicitly set the field names to the encoding/json defaults. The struct field names and types should all match up 1:1 to the old struct definitions.

- Human readable description for the release notes

 - Swagger definitions for `NetworkSummary` and `NetworkInspect` have been added to the Swagger spec describing the Engine API.

- A picture of a cute animal (not mandatory but encouraged)

@corhere corhere added this to the 29.0.0 milestone Aug 29, 2025
@corhere corhere requested a review from tianon as a code owner August 29, 2025 19:24
@corhere corhere added area/api API impact/changelog kind/refactor PR's that refactor, or clean-up code labels Aug 29, 2025
@corhere corhere force-pushed the network-summary-swagger branch 2 times, most recently from 5416d6c to 32fde53 Compare August 29, 2025 20:01
@thaJeztah
Copy link
Member

This one needs a rebase now, @corhere

Taken verbatim from
https://github.com/go-swagger/go-swagger/blob/eee6eaf67f2f342970d277c85ba73f05aed4d114/generator/templates/structfield.gotmpl
so the alterations from the upstream template can be easily diffed.

Signed-off-by: Cory Snider <csnider@mirantis.com>
@corhere corhere force-pushed the network-summary-swagger branch from c326d03 to f4acbfc Compare September 4, 2025 21:00
@corhere corhere changed the title api/types/network: generate network.Summary struct from Swagger spec api/types/network: generate network-inspect struct definitions from Swagger spec Sep 4, 2025
@thaJeztah

This comment was marked as outdated.

@corhere corhere force-pushed the network-summary-swagger branch 4 times, most recently from 502b108 to b895ee6 Compare September 4, 2025 23:40
Replace the hand-rolled Network, Summary and Inspect struct types in
api/types/network with types generated from the Swagger definition.

Disable the generation of all unwanted marshalers and unmarshalers.

Signed-off-by: Cory Snider <csnider@mirantis.com>
Replace hand-rolled struct definitions for api/types/network with
types generated from the Swagger definitions:
  - ConfigReference
  - EndpointResource
  - NetworkingConfig
  - PeerInfo
  - ServiceInfo
  - Task

Add Swagger definitions for ServiceInfo and Task.

Signed-off-by: Cory Snider <csnider@mirantis.com>
@corhere corhere force-pushed the network-summary-swagger branch from b895ee6 to e656f39 Compare September 4, 2025 23:46
@corhere corhere requested a review from thaJeztah September 5, 2025 09:13
Copy link
Member
@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - much better without the weird marshal functions and dependencies

Comment on lines +5 to +7
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one was not in the template we forked, right? (Was looking if we could get rid of it, because it's really just noise on a file with the standard format Code generated by go-swagger; DO NOT EDIT. header 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +37 to +41
// Address represents an IP address
type Address struct {
Addr string
PrefixLen int
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got curious why we had this type, but looks like it's only used in fields that are deprecated and those are being removed by @akerouanton in #50846

Looks like that PR doesn't remove the Address type though; probably should 🤔

description:
ID of the peer-node in the Swarm cluster.
type: "string"
x-omitempty: false
Copy link
Member
@thaJeztah thaJeztah Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x-omitempty: false is not the default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh! that's surprising; or does it expect all fields that are not "nullable" to be added to required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless the JSON schema says an object property is required, its optional per the JSON Schema Validation spec.

An object instance is valid against this keyword if every item in the array is the name of a property in the instance.

Go-swagger is taking that to its logical conclusion: properties that aren't required are only serialized if they are set to a meaningful value. We have the choice to use whichever of required or x-omitempty is more appropriate for the circumstances.

The extension does not force a required field to get the “omitempty” modifier.

Given our tendency to deprecate and remove API fields, I think x-omitempty is more appropriate than required when describing the Engine API for the same reason required fields are harmful in protobuf land: what's required today may be deprecated tomorrow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that makes sense! And definitely, x-omitempty is more suitable to what our intent is. I was wondering if other tooling would treat it different.

@thaJeztah
Copy link
Member

@akerouanton PTAL - this one will conflict with your other PR; not sure what's the best order to merge; this one first or yours first.

We also need to sync the v1.52 copy of the swagger file in the docs directory; ideally as part of this PR, but otherwise a follow-up is probably fine.

@thaJeztah
Copy link
Member

Chatted with @akerouanton and he's ok with doing the rebase on his PR, so let me bring this one in

@thaJeztah thaJeztah merged commit 4e23f5f into moby:master Sep 5, 2025
215 of 217 checks passed
@corhere corhere deleted the network-summary-swagger branch September 5, 2025 17:22
@thompson-shaun thompson-shaun moved this from New to Complete in 🔦 Maintainer spotlight Sep 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api API impact/api impact/changelog kind/refactor PR's that refactor, or clean-up code
Projects
Archived in project
Development

Successfully merging this pull request may close these 3AA2 issues.

3 participants
0