Skip to content

Commit

Permalink
Lifts validations (#1507)
Browse files Browse the repository at this point in the history
- lifts nested validations with allOf, additionalProperties, additionalItems, maps and slices of such constructs
- ensures that empty Validate() func remain empty when there is no validation (e.g. just return nil)
- fixes incorrect codegen with empty objects and additionalProperties (conflict on nullable)
- fixes missing validations with additionalProperties (aliased types, nested maps ending with a $ref)
- removes NeedsValidation, NeedsRequired
- adds codegen UT, to run and assert both the default flatten and the --skip-flatten option (expand)
- fixes unmarshaller for anonymous nested allOf
- expand UT coverage on model.go and types.go

Fixes:

- fixes #1487 (nested validations with additionalItems)
- fixes #1479 (nested validations with allOf)
- fixes #1453 (maps of slices, empty objects)

For the following issues, it is difficult to tell if they were already fixed or not.
Anyhow, this commit asserts the codegen for those, with spec flatten (default) and expand option.

Acknowledges past fixes (with possible slight codegen modifications) and adds non-reg UT:

- fixes #1198 - additionalProperties
- fixes #979  - allOf
- fixes #1042 - allOf

The following (polymorphic cases) are acknowledged only with the default spec flatten option:

- fixes #606  - polymorphic types (and non-reg on duplicate #1336)
- fixes #842  - polymorphic types
- fixes #607  - polymorphic types

Contributes to :

- #1232 - allOf
- #1233 - polymorphic types
  • Loading branch information
fredbi authored and casualjim committed May 7, 2018
1 parent 174f825 commit 58db231
Show file tree
Hide file tree
Showing 50 changed files with 16,318 additions and 266 deletions.
42 changes: 42 additions & 0 deletions fixtures/bugs/1042/fixture-1042-2.yaml
@@ -0,0 +1,42 @@
swagger: "2.0"
info:
title: allOf marshalling
description: |
when the specification incorrectly defines the allOf,
generated unmarshalling is wrong.
This fixture asserts that with correct spec, the generated models are correct.
version: "0.0"

basePath: "/test"

paths:
"/allof":
get:
responses:
'200':
description: test
schema:
"$ref": "#/definitions/ExtendedErrorModel"

definitions:
ErrorModel:
type: object
required:
- message
- code
properties:
message:
type: string
code:
type: integer
minimum: 100
maximum: 600
ExtendedErrorModel:
allOf:
- $ref: '#/definitions/ErrorModel'
- type: object
required:
- rootCause
properties:
rootCause:
type: string
62 changes: 62 additions & 0 deletions fixtures/bugs/1042/fixture-1042-bad.json
@@ -0,0 +1,62 @@
{
"swagger": "2.0",
"info": {
"title": "allOf marshalling",
"description": "when the specification incorrectly defines the allOf,\ngenerated unmarshalling is wrong.\nThis fixture asserts that with correct spec, the generated models are correct.\n",
"version": "0.0"
},
"basePath": "/test",
"paths": {
"/allof": {
"get": {
"responses": {
"200": {
"description": "test",
"schema": {
"$ref": "#/definitions/B"
}
}
}
}
}
},
"definitions": {
"A": {
"description": "a first definition A",
"type": "object",
"properties": {
"f1": {
"type": "string"
},
"f2": {
"type": "string"
}
}
},
"B": {
"description": "a second definition B, which extends A",
"type": "object",
"allOf": [
{
"$ref": "#/definitions/A"
},
null
],
"required": [
"f3",
"f4"
],
"properties": {
"f3": {
"type": "string"
},
"f4": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
47 changes: 47 additions & 0 deletions fixtures/bugs/1042/fixture-1042-bad.yaml
@@ -0,0 +1,47 @@
swagger: "2.0"
info:
title: allOf marshalling
description: |
when the specification incorrectly defines the allOf,
generated unmarshalling is wrong.
This fixture asserts that with correct spec, the generated models are correct.
version: "0.0"

basePath: "/test"

paths:
"/allof":
get:
responses:
'200':
description: test
schema:
"$ref": "#/definitions/B"

definitions:
A:
description: a first definition A
type: object
properties:
f1:
type: string
f2:
type: string

B:
description: a second definition B, which extends A
type: object
allOf:
- "$ref": "#/definitions/A"
-
required:
- f3
- f4
properties:
f3:
type: string
f4:
type: array
items:
type: string

63 changes: 63 additions & 0 deletions fixtures/bugs/1042/fixture-1042.json
@@ -0,0 +1,63 @@
{
"swagger": "2.0",
"info": {
"title": "allOf marshalling",
"description": "when the specification incorrectly defines the allOf,\ngenerated unmarshalling is wrong.\nThis fixture asserts that with correct spec, the generated models are correct.\n",
"version": "0.0"
},
"basePath": "/test",
"paths": {
"/allof": {
"get": {
"responses": {
"200": {
"description": "test",
"schema": {
"$ref": "#/definitions/B"
}
}
}
}
}
},
"definitions": {
"A": {
"description": "a first definition A",
"type": "object",
"properties": {
"f1": {
"type": "string"
},
"f2": {
"type": "string"
}
}
},
"B": {
"description": "a second definition B, which extends A",
"type": "object",
"allOf": [
{
"$ref": "#/definitions/A"
},
{
"required": [
"f3",
"f4"
],
"properties": {
"f3": {
"type": "string"
},
"f4": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
]
}
}
}
46 changes: 46 additions & 0 deletions fixtures/bugs/1042/fixture-1042.yaml
@@ -0,0 +1,46 @@
swagger: "2.0"
info:
title: allOf marshalling
description: |
when the specification incorrectly defines the allOf,
generated unmarshalling is wrong.
This fixture asserts that with correct spec, the generated models are correct.
version: "0.0"

basePath: "/test"

paths:
"/allof":
get:
responses:
'200':
description: test
schema:
"$ref": "#/definitions/B"

definitions:
A:
description: a first definition A
type: object
properties:
f1:
type: string
f2:
type: string

B:
description: a second definition B, which extends A
type: object
allOf:
- "$ref": "#/definitions/A"
-
required:
- f3
- f4
properties:
f3:
type: string
f4:
type: array
items:
type: string
94 changes: 94 additions & 0 deletions fixtures/bugs/1042/gen-fixtures.sh
@@ -0,0 +1,94 @@
#! /bin/bash
if [[ ${1} == "--clean" ]] ; then
clean=1
fi
# Acknowledgements
testcases="${testcases} fixture-1042.yaml"
testcases="${testcases} fixture-1042-2.yaml"
for testcase in ${testcases} ; do
spec=${testcase}
testcase=`basename ${testcase}`
target=./gen-${testcase%.*}
serverName="codegensrv"
rm -rf ${target}
mkdir ${target}
echo "Model generation for ${spec}"
swagger generate model --skip-validation --spec ${spec} --target ${target} --output=${testcase%.*}.log
# 1>x.log 2>&1
#
if [[ $? != 0 ]] ; then
echo "Generation failed for ${spec}"
exit 1
fi
echo "${spec}: Generation OK"
if [[ ! -d ${target}/models ]] ; then
echo "No model in this spec! Skipped"
else
(cd ${target}/models; go build)
if [[ $? != 0 ]] ; then
echo "Build failed for ${spec}"
exit 1
fi
echo "${spec}: Build OK"
if [[ -n ${clean} ]] ; then
rm -rf ${target}
fi
fi
done
exit
testcases=`cd ../../codegen;ls -1|grep -vE 'azure|bitbucket|existing-model|issue72|todolist.simple.yml'`
#testcases=${testcases}" fixture-1062.json fixture-984.yaml"
#testcases=`cd ../../codegen;ls -1|grep 'todolist.enums.yml'`
for testcase in ${testcases} ; do
target=./gen-${testcase%.*}
if [[ -f ../../codegen/${testcase} ]] ; then
spec=../../codegen/${testcase}
else
spec=${testcase}
fi
serverName="nrcodegen"
rm -rf ${target}
mkdir ${target}
echo "Server generation for ${spec}"
swagger generate server --skip-validation --spec ${spec} --target ${target} --name=${serverName} 1>${testcase%.*}.log 2>&1
#--output=${testcase%.*}.log
if [[ $? != 0 ]] ; then
echo "Generation failed for ${spec}"
exit 1
fi
echo "${spec}: Generation OK"
(cd ${target}/cmd/${serverName}"-server"; go build)
if [[ $? != 0 ]] ; then
echo "Build failed for ${spec}"
exit 1
fi
echo "${spec}: Build OK"
if [[ -n ${clean} ]] ; then
rm -rf ${target}
fi
done
# TODO(fredbi): enable non reg again
testcases=
for testcase in ${testcases} ; do
target=./gen-${testcase%.*}
spec=./${testcase}
serverName="bugfix"
rm -rf ${target}
mkdir ${target}
echo "Generation for ${spec}"
swagger generate server --spec ${spec} --target ${target} --quiet --name=${serverName}
if [[ $? != 0 ]] ; then
echo "Generation failed for ${spec}"
exit 1
fi
echo "${spec}: Generation OK"
(cd ${target}/cmd/${serverName}"-server"; go build)
if [[ $? != 0 ]] ; then
echo "Build failed for ${spec}"
exit 1
fi
echo "${spec}: Build OK"
if [[ -n ${clean} ]] ; then
rm -rf ${target}
fi
done

0 comments on commit 58db231

Please sign in to comment.