Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revamped jsonschema with validations and examples #4951

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ pkg/protocols/headless/engine/.cache
**/*-cache
/fuzzplayground
integration_tests/fuzzplayground

/docgen
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ fuzzplayground:
memogen:
$(GOBUILD) $(GOFLAGS) -ldflags '$(LDFLAGS)' -o "memogen" cmd/memogen/memogen.go
./memogen -src pkg/js/libs -tpl cmd/memogen/function.tpl
genschema:
rm -f nuclei-jsonschema.json docgen 2>/dev/null
$(GOBUILD) $(GOFLAGS) -ldflags '$(LDFLAGS)' -o "docgen" cmd/docgen/docgen.go
./docgen /dev/null nuclei-jsonschema.json

33 changes: 32 additions & 1 deletion cmd/docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func main() {
}

// Generate jsonschema
r := &jsonschema.Reflector{}
r := &jsonschema.Reflector{
BaseSchemaID: jsonschema.ID("https://nuclei.projectdiscovery.io/"),
}
jsonschemaData := r.Reflect(&templates.Template{})

var buf bytes.Buffer
Expand All @@ -44,8 +46,37 @@ func main() {
for _, match := range pathRegex.FindAllStringSubmatch(schema, -1) {
schema = strings.ReplaceAll(schema, match[0], match[1])
}
var m map[string]interface{}
err = json.Unmarshal([]byte(schema), &m)
if err != nil {
log.Fatalf("Could not unmarshal jsonschema: %s\n", err)
}

// patch the schema to enable markdown Descriptions in monaco and vscode
updateDescriptionKeyName("", m)

schemax, err := json.MarshalIndent(m, "", " ")
if err != nil {
log.Fatalf("Could not marshal jsonschema: %s\n", err)
}
schema = string(schemax)

err = os.WriteFile(os.Args[2], []byte(schema), 0644)
if err != nil {
log.Fatalf("Could not write jsonschema: %s\n", err)
}
}

// will recursively find and replace/rename PropName in description
func updateDescriptionKeyName(parent string, m map[string]interface{}) {
for k, v := range m {
if k == "description" && parent != "properties" {
delete(m, k)
m["markdownDescription"] = v
}
// if v is of type object then recursively call this function
if vMap, ok := v.(map[string]interface{}); ok {
updateDescriptionKeyName(k, vMap)
}
}
}
Loading
Loading