Skip to content

Commit

Permalink
Support recursive references (#74)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
julienduchesne authored Oct 26, 2021
1 parent eba24a3 commit 23fd6e9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 12 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand All @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand All @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand All @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand All @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand All @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand All @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand All @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand All @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand All @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand Down Expand Up @@ -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": "[email protected]"
"GIT_COMMITTER_NAME": "jsonnet-libs-bot"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)" \
Expand Down
1 change: 1 addition & 0 deletions jsonnet/github_action.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ local libJob(name) = {
GIT_COMMITTER_EMAIL: '[email protected]',
SSH_KEY: '${{ secrets.DEPLOY_KEY }}',
GEN_COMMIT: "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}",
DIFF: 'true',
},
},
],
Expand Down
6 changes: 6 additions & 0 deletions libs/argo-workflows/config.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
]
)
29 changes: 18 additions & 11 deletions pkg/swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,32 @@ 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
}

return d
}

func get(prop *Schema, defs Definitions) *Schema {
func (s *Swagger) get(prop *Schema) *Schema {
if prop == nil || prop.DollarRef == nil {
return prop
}
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions scripts/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 23fd6e9

Please sign in to comment.