From 23fd6e96f88cd0c163e0d3be6abbe2681bb88e9d Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Tue, 26 Oct 2021 06:48:28 -0400 Subject: [PATCH] Support recursive references (#74) * Support recursive references While testing a change I made to the argo-workflows jsonschema, I found this: argo-workflows introduces a definition which is recursive The `make libs/argo-workflows` dies from stack overflow This PR adds a resolve map that saves the *Schema objects so that they are reused instead of being resolved infinitely Submitting this for early review, I will merge this once Argo Workflows 3.2.0 is released * Add diff * Use 3.2.2 tag --- .github/workflows/main.yml | 11 +++++++++++ Dockerfile | 2 +- Makefile | 1 + jsonnet/github_action.jsonnet | 1 + libs/argo-workflows/config.jsonnet | 6 ++++++ pkg/swagger/swagger.go | 29 ++++++++++++++++++----------- scripts/gen.sh | 10 ++++++++++ 7 files changed, 48 insertions(+), 12 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3d469b38..c2622af3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,6 +7,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -19,6 +20,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -31,6 +33,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -43,6 +46,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -55,6 +59,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -77,6 +82,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -89,6 +95,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -101,6 +108,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -113,6 +121,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -125,6 +134,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" @@ -217,6 +227,7 @@ "steps": - "uses": "actions/checkout@v2" - "env": + "DIFF": "true" "GEN_COMMIT": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}" "GIT_COMMITTER_EMAIL": "86770550+jsonnet-libs-bot@users.noreply.github.com" "GIT_COMMITTER_NAME": "jsonnet-libs-bot" diff --git a/Dockerfile b/Dockerfile index 598d4ab3..b3abef29 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,7 +26,7 @@ FROM alpine:3.14 WORKDIR /app -RUN apk add --no-cache bash curl git openssh +RUN apk add --no-cache bash curl git openssh diffutils ENV KUBECONFIG=/app/kubeconfig/kube-config.yaml RUN chmod a+w /app diff --git a/Makefile b/Makefile index 0768c620..e5e0edaa 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ libs/*: ./bin/docker.sh \ -v $(shell realpath $@):/config \ -v $(ABS_OUTPUT_DIR):/output \ + -e DIFF \ -e GEN_COMMIT="$(GEN_COMMIT)" \ -e GITHUB_SHA="$(GITHUB_SHA)" \ -e GIT_AUTHOR_NAME="$(GIT_AUTHOR_NAME)" \ diff --git a/jsonnet/github_action.jsonnet b/jsonnet/github_action.jsonnet index be8fa891..4a132ed0 100644 --- a/jsonnet/github_action.jsonnet +++ b/jsonnet/github_action.jsonnet @@ -59,6 +59,7 @@ local libJob(name) = { GIT_COMMITTER_EMAIL: '86770550+jsonnet-libs-bot@users.noreply.github.com', SSH_KEY: '${{ secrets.DEPLOY_KEY }}', GEN_COMMIT: "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}", + DIFF: 'true', }, }, ], diff --git a/libs/argo-workflows/config.jsonnet b/libs/argo-workflows/config.jsonnet index 06c69082..ee0eaa42 100644 --- a/libs/argo-workflows/config.jsonnet +++ b/libs/argo-workflows/config.jsonnet @@ -9,5 +9,11 @@ config.new( openapi: 'https://raw.githubusercontent.com/argoproj/argo-workflows/v3.1.12/api/jsonschema/schema.json', localName: 'argo_workflows', }, + { + output: '3.2', + prefix: '^io\\.argoproj\\..*', + openapi: 'https://raw.githubusercontent.com/argoproj/argo-workflows/v3.2.2/api/jsonschema/schema.json', + localName: 'argo_workflows', + }, ] ) diff --git a/pkg/swagger/swagger.go b/pkg/swagger/swagger.go index 6c970f73..b0209fac 100644 --- a/pkg/swagger/swagger.go +++ b/pkg/swagger/swagger.go @@ -36,18 +36,24 @@ func Load(data []byte) (*Swagger, error) { if err := json.Unmarshal(data, &s); err != nil { return nil, err } + s.resolveMap = map[string]*Schema{} for k, def := range s.Definitions { - s.Definitions[k] = resolveRefs(def, s.Definitions) + s.Definitions[k] = s.resolveRefs(def) } return &s, nil } -func resolveRefs(d *Schema, defs Definitions) *Schema { +type Swagger struct { + resolveMap map[string]*Schema // store objects in a map in order to support recursive references + Definitions Definitions `json:"definitions"` +} + +func (s *Swagger) resolveRefs(d *Schema) *Schema { for key, prop := range d.Props { - resolved := get(prop, defs) - resolved.Items = get(resolved.Items, defs) + resolved := s.get(prop) + resolved.Items = s.get(resolved.Items) d.Props[key] = resolved } @@ -55,7 +61,7 @@ func resolveRefs(d *Schema, defs Definitions) *Schema { return d } -func get(prop *Schema, defs Definitions) *Schema { +func (s *Swagger) get(prop *Schema) *Schema { if prop == nil || prop.DollarRef == nil { return prop } @@ -65,13 +71,14 @@ func get(prop *Schema, defs Definitions) *Schema { return prop } - rs := defs[ref] + if rs, ok := s.resolveMap[ref]; ok { + return rs + } + rs := s.Definitions[ref] rs.ResolvedRef = ref - return resolveRefs(rs, defs) -} - -type Swagger struct { - Definitions Definitions `json:"definitions"` + s.resolveMap[ref] = rs + rs = s.resolveRefs(rs) + return rs } type Definitions map[string]*Schema diff --git a/scripts/gen.sh b/scripts/gen.sh index b654b8c5..3d6749d9 100755 --- a/scripts/gen.sh +++ b/scripts/gen.sh @@ -131,3 +131,13 @@ else fi popd fi + + +if [ "${DIFF}" == "true" ]; then + echo "Diffing..." + DIFF_DIR="${TMPDIR:-/tmp/}${REPO}" + # HTTPS should always be usable without creds + git clone --depth 1 "https://${REPO}" "${DIFF_DIR}" + + (diff -r --exclude .git "${DIFF_DIR}" "${OUTPUT_DIR}" && echo "No diff!") || echo "There's a diff!" +fi \ No newline at end of file