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

Nested struct support? #171

Open
andrewpollock opened this issue Nov 21, 2023 · 1 comment
Open

Nested struct support? #171

andrewpollock opened this issue Nov 21, 2023 · 1 comment

Comments

@andrewpollock
Copy link

I'm here after trying and failing to use https://github.com/a-h/generate (for my use case), running into a fresher variation of a-h/generate#67 there...

My use case is similar to what's described in aforementioned issue: I'd like to generate structs from https://csrc.nist.gov/schema/nvd/api/2.0/cve_api_json_2.0.schema and that seems to involve something Moderately Complex (I am far from a JSON spec guru).

Where schema-generate -p cves /tmp/cve_api_json_2.0.schema generates:

// JSONSchemaForNVDVulnerabilityDataAPIVersion201 
type JSONSchemaForNVDVulnerabilityDataAPIVersion201 struct {
  Format string `json:"format"`
  ResultsPerPage int `json:"resultsPerPage"`
  StartIndex int `json:"startIndex"`
  Timestamp string `json:"timestamp"`
  TotalResults int `json:"totalResults"`
  Version string `json:"version"`

  // NVD feed array of CVE
  Vulnerabilities []*DefCveItem `json:"vulnerabilities"`
}

and then fails define DefCveItem at all, producing something that isn't compiler-satisfactory,

with go-jsonschema -p cves /tmp/cve_api_json_2.0.schema at least I get:

type CveApiJson20Schema struct {
        // Format corresponds to the JSON schema field "format".
        Format string `json:"format" yaml:"format" mapstructure:"format"`

        // ResultsPerPage corresponds to the JSON schema field "resultsPerPage".
        ResultsPerPage int `json:"resultsPerPage" yaml:"resultsPerPage" mapstructure:"resultsPerPage"`

        // StartIndex corresponds to the JSON schema field "startIndex".
        StartIndex int `json:"startIndex" yaml:"startIndex" mapstructure:"startIndex"`

        // Timestamp corresponds to the JSON schema field "timestamp".
        Timestamp time.Time `json:"timestamp" yaml:"timestamp" mapstructure:"timestamp"`

        // TotalResults corresponds to the JSON schema field "totalResults".
        TotalResults int `json:"totalResults" yaml:"totalResults" mapstructure:"totalResults"`

        // Version corresponds to the JSON schema field "version".
        Version string `json:"version" yaml:"version" mapstructure:"version"`

        // NVD feed array of CVE
        Vulnerabilities []DefCveItem `json:"vulnerabilities" yaml:"vulnerabilities" mapstructure:"vulnerabilities"`
}

type DefCveItem interface{}

this just leaves the definition of DefCveItem a little... non-specific for my liking/needs.

@nick-jones
Copy link
Contributor

nick-jones commented Dec 5, 2023

Definitions are handled. What seems to be tripping things up is the lack of type declaration for the defintion of def_cve_item - I'm no expert on the various spec versions, but it looks like they should be explicitly stated in draft-07

After manually patching "type": "object" in, I get the following:

type DefCveItem struct {
	// Cve corresponds to the JSON schema field "cve".
	Cve CveItem `json:"cve" yaml:"cve" mapstructure:"cve"`
}

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

No branches or pull requests

2 participants