Skip to content

Commit

Permalink
feat: add 'additionalProperties: false' to the generated JSON schema …
Browse files Browse the repository at this point in the history
…if command-line flag is set
  • Loading branch information
hajowieland committed Feb 21, 2024
1 parent 4dc7a4e commit f7e7f8b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
9 changes: 5 additions & 4 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func GenerateJsonSchema(config *Config) error {

// Create a temporary Schema to merge from the nodes
tempSchema := &Schema{
Type: "object",
Properties: properties,
Required: required,
Type: "object",
Properties: properties,
Required: required,
AdditionalProperties: &config.noAdditionalProperties,
}

// Merge with existing data
Expand All @@ -63,7 +64,7 @@ func GenerateJsonSchema(config *Config) error {
}

// Convert merged Schema into a JSON Schema compliant map
jsonSchemaMap, err := convertSchemaToMap(mergedSchema)
jsonSchemaMap, err := convertSchemaToMap(mergedSchema, config.noAdditionalProperties)
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ func mergeSchemas(dest, src *Schema) *Schema {
return dest
}

func convertSchemaToMap(schema *Schema) (map[string]interface{}, error) {
return convertSchemaToMapRec(schema, make(map[uintptr]bool))
func convertSchemaToMap(schema *Schema, noAdditionalProperties bool) (map[string]interface{}, error) {
return convertSchemaToMapRec(schema, make(map[uintptr]bool), noAdditionalProperties)
}

func convertSchemaToMapRec(schema *Schema, visited map[uintptr]bool) (map[string]interface{}, error) {
func convertSchemaToMapRec(schema *Schema, visited map[uintptr]bool, noAdditionalProperties bool) (map[string]interface{}, error) {
if schema == nil {
return nil, nil
}
Expand All @@ -106,6 +106,11 @@ func convertSchemaToMapRec(schema *Schema, visited map[uintptr]bool) (map[string

schemaMap := make(map[string]interface{})

// Apply additionalProperties: false if noAdditionalProperties is true
if noAdditionalProperties && schema.Type == "object" {
schemaMap["additionalProperties"] = false
}

// Scalars
if schema.Type != "" {
schemaMap["type"] = schema.Type
Expand Down Expand Up @@ -160,7 +165,7 @@ func convertSchemaToMapRec(schema *Schema, visited map[uintptr]bool) (map[string

// Nested Schemas
if schema.Items != nil {
itemsMap, err := convertSchemaToMapRec(schema.Items, visited)
itemsMap, err := convertSchemaToMapRec(schema.Items, visited, noAdditionalProperties)
if err != nil {
return nil, err
}
Expand All @@ -169,7 +174,7 @@ func convertSchemaToMapRec(schema *Schema, visited map[uintptr]bool) (map[string
if schema.Properties != nil {
propertiesMap := make(map[string]interface{})
for propName, propSchema := range schema.Properties {
propMap, err := convertSchemaToMapRec(propSchema, visited)
propMap, err := convertSchemaToMapRec(propSchema, visited, noAdditionalProperties)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit f7e7f8b

Please sign in to comment.