-
Notifications
You must be signed in to change notification settings - Fork 60
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
tools/importer
: skip build model if input schema is only for property
#3041
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae47b9c
bug: adding a poc for #2675 / duplicate models
tombuildsstuff e44faa5
parseObjectDefinition should not parse for top model if it is from de…
wuxu92 2a56e07
code clean, update unit test
wuxu92 c94cc5d
update comment
wuxu92 f9de677
update comment
wuxu92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -148,7 +148,8 @@ func (d *SwaggerDefinition) detailsForField(modelName string, propertyName strin | |||||||
} | ||||||||
|
||||||||
// first get the object definition | ||||||||
objectDefinition, nestedResult, err := d.parseObjectDefinition(modelName, propertyName, &value, result) | ||||||||
parsingModel := false | ||||||||
objectDefinition, nestedResult, err := d.parseObjectDefinition(modelName, propertyName, &value, result, parsingModel) | ||||||||
if err != nil { | ||||||||
return nil, nil, fmt.Errorf("parsing object definition: %+v", err) | ||||||||
} | ||||||||
|
@@ -451,7 +452,13 @@ func (d *SwaggerDefinition) findAncestorType(input spec.Schema) (*string, *strin | |||||||
return nil, nil, nil | ||||||||
} | ||||||||
|
||||||||
func (d SwaggerDefinition) parseObjectDefinition(modelName, propertyName string, input *spec.Schema, known internal.ParseResult) (*models.ObjectDefinition, *internal.ParseResult, error) { | ||||||||
// if `inputForModel` is false, it means the `input` schema cannot be used to parse the model of `modelName` | ||||||||
func (d SwaggerDefinition) parseObjectDefinition( | ||||||||
modelName, propertyName string, | ||||||||
input *spec.Schema, | ||||||||
known internal.ParseResult, | ||||||||
parsingModel bool, | ||||||||
) (*models.ObjectDefinition, *internal.ParseResult, error) { | ||||||||
// find the object and any models and constants etc we can find | ||||||||
// however _don't_ look for discriminator implementations - since that should be done when we're completely done | ||||||||
result := internal.ParseResult{ | ||||||||
|
@@ -509,7 +516,7 @@ func (d SwaggerDefinition) parseObjectDefinition(modelName, propertyName string, | |||||||
} | ||||||||
|
||||||||
// then call ourselves to work out what to do with it | ||||||||
objectDefinition, nestedResult, err := d.parseObjectDefinition(*objectName, propertyName, topLevelObject, knownIncludingPlaceholder) | ||||||||
objectDefinition, nestedResult, err := d.parseObjectDefinition(*objectName, propertyName, topLevelObject, knownIncludingPlaceholder, true) | ||||||||
if err != nil { | ||||||||
return nil, nil, err | ||||||||
} | ||||||||
|
@@ -519,18 +526,19 @@ func (d SwaggerDefinition) parseObjectDefinition(modelName, propertyName string, | |||||||
return objectDefinition, nestedResult, nil | ||||||||
} | ||||||||
|
||||||||
// if it's an inlined model, pull it out and return that | ||||||||
// note: some models can just be references to other models | ||||||||
// however we should only do this when we're parsing a model (`parsingModel`) directly rather than when parsing a model from a field - and only if we haven't already parsed this model | ||||||||
if len(input.Properties) > 0 || len(input.AllOf) > 0 { | ||||||||
// special-case: if the model has no properties and inherits from one model | ||||||||
// then just return that object instead, there's no point creating the wrapper type | ||||||||
if len(input.Properties) == 0 && len(input.AllOf) == 1 { | ||||||||
inheritedModel := input.AllOf[0] | ||||||||
return d.parseObjectDefinition(inheritedModel.Title, propertyName, &inheritedModel, result) | ||||||||
return d.parseObjectDefinition(inheritedModel.Title, propertyName, &inheritedModel, result, true) | ||||||||
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
} | ||||||||
|
||||||||
// check for / avoid circular references | ||||||||
if _, ok := result.Models[modelName]; !ok { | ||||||||
// only parse model when modelName equals PropertyName, otherwise the `input` Schema may not for the modelName | ||||||||
// if parsing a top-level or an inlined model, should pass the propertyName just the same as the modelName | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this'd be clearer as:
Suggested change
|
||||||||
if _, ok := result.Models[modelName]; !ok && parsingModel { | ||||||||
nestedResult, err := d.parseModel(modelName, *input) | ||||||||
if err != nil { | ||||||||
return nil, nil, fmt.Errorf("parsing object from inlined model %q: %+v", modelName, err) | ||||||||
|
@@ -568,7 +576,7 @@ func (d SwaggerDefinition) parseObjectDefinition(modelName, propertyName string, | |||||||
innerModelName = input.AdditionalProperties.Schema.Title | ||||||||
} | ||||||||
|
||||||||
nestedItem, nestedResult, err := d.parseObjectDefinition(innerModelName, propertyName, input.AdditionalProperties.Schema, result) | ||||||||
nestedItem, nestedResult, err := d.parseObjectDefinition(innerModelName, propertyName, input.AdditionalProperties.Schema, result, true) | ||||||||
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
if err != nil { | ||||||||
return nil, nil, fmt.Errorf("parsing nested item for dictionary: %+v", err) | ||||||||
} | ||||||||
|
@@ -591,7 +599,7 @@ func (d SwaggerDefinition) parseObjectDefinition(modelName, propertyName string, | |||||||
inlinedName = fmt.Sprintf("%s%sInlined", cleanup.NormalizeName(modelName), cleanup.NormalizeName(propertyName)) | ||||||||
} | ||||||||
|
||||||||
nestedItem, nestedResult, err := d.parseObjectDefinition(inlinedName, propertyName, input.Items.Schema, result) | ||||||||
nestedItem, nestedResult, err := d.parseObjectDefinition(inlinedName, propertyName, input.Items.Schema, result, true) | ||||||||
tombuildsstuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
if err != nil { | ||||||||
return nil, nil, fmt.Errorf("parsing nested item for array: %+v", err) | ||||||||
} | ||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
...s/importer-rest-api-specs/components/parser/testdata/models_bug_2675_duplicate_model.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"title": "Example", | ||
"description": "Example", | ||
"version": "2020-01-01" | ||
}, | ||
"host": "management.mysite.com", | ||
"schemes": [ | ||
"https" | ||
], | ||
"consumes": [ | ||
"application/json" | ||
], | ||
"produces": [ | ||
"application/json" | ||
], | ||
"security": [], | ||
"securityDefinitions": {}, | ||
"paths": { | ||
"/environments/{environmentName}": { | ||
"get": { | ||
"tags": [ | ||
"Example" | ||
], | ||
"description": "Gets an example environment.", | ||
"parameters": [ | ||
{ | ||
"name": "environmentName", | ||
"in": "path", | ||
"required": true, | ||
"type": "string", | ||
"x-ms-parameter-location": "method" | ||
} | ||
], | ||
"operationId": "Example_Get", | ||
"responses": { | ||
"200": { | ||
"description": "OK. The request has succeeded.", | ||
"schema": { | ||
"$ref": "#/definitions/ExampleEnvironment" | ||
} | ||
} | ||
} | ||
}, | ||
"put": { | ||
"tags": [ | ||
"Example" | ||
], | ||
"description": "Creates or updates an example environment.", | ||
"parameters": [ | ||
{ | ||
"name": "environmentName", | ||
"in": "path", | ||
"required": true, | ||
"type": "string", | ||
"x-ms-parameter-location": "method" | ||
}, | ||
{ | ||
"name": "body", | ||
"in": "body", | ||
"required": true, | ||
"schema": { | ||
"$ref": "#/definitions/ExampleEnvironment" | ||
} | ||
} | ||
], | ||
"operationId": "Example_CreateOrUpdate", | ||
"responses": { | ||
"200": { | ||
"description": "Succeeded", | ||
"schema": { | ||
"$ref": "#/definitions/ExampleEnvironment" | ||
} | ||
} | ||
} | ||
}, | ||
"patch": { | ||
"tags": [ | ||
"Example" | ||
], | ||
"description": "Patches an example environment.", | ||
"parameters": [ | ||
{ | ||
"name": "environmentName", | ||
"in": "path", | ||
"required": true, | ||
"type": "string", | ||
"x-ms-parameter-location": "method" | ||
}, | ||
{ | ||
"name": "body", | ||
"in": "body", | ||
"description": "Updatable project environment type properties.", | ||
"required": true, | ||
"schema": { | ||
"$ref": "#/definitions/ExampleEnvironmentUpdate" | ||
} | ||
} | ||
], | ||
"operationId": "Example_Update", | ||
"responses": { | ||
"200": { | ||
"description": "The resource was updated.", | ||
"schema": { | ||
"$ref": "#/definitions/ExampleEnvironment" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"ExampleEnvironment": { | ||
"type": "object", | ||
"properties": { | ||
"properties": { | ||
"x-ms-client-flatten": true, | ||
"$ref": "#/definitions/ExampleEnvironmentProperties" | ||
}, | ||
"location": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"ExampleEnvironmentUpdateProperties": { | ||
"type": "object", | ||
"properties": { | ||
"deploymentTargetId": { | ||
"type": "string" | ||
}, | ||
"creatorRoleAssignment": { | ||
"type": "object", | ||
"properties": { | ||
"roles": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"$ref": "#/definitions/EnvironmentRole" | ||
} | ||
} | ||
} | ||
}, | ||
"userRoleAssignments": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"$ref": "#/definitions/UserRoleAssignment" | ||
} | ||
} | ||
} | ||
}, | ||
"ExampleEnvironmentProperties": { | ||
"type": "object", | ||
"allOf": [ | ||
{ | ||
"$ref": "#/definitions/ExampleEnvironmentUpdateProperties" | ||
} | ||
], | ||
"properties": { | ||
"provisioningState": { | ||
"type": "string", | ||
"readOnly": true | ||
} | ||
} | ||
}, | ||
"ExampleEnvironmentUpdate": { | ||
"type": "object", | ||
"properties": { | ||
"properties": { | ||
"x-ms-client-flatten": true, | ||
"$ref": "#/definitions/ExampleEnvironmentUpdateProperties" | ||
}, | ||
"example": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"UserRoleAssignment": { | ||
"type": "object", | ||
"x-ms-client-name": "userRoleAssignmentValue", | ||
"properties": { | ||
"roles": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"$ref": "#/definitions/EnvironmentRole" | ||
} | ||
} | ||
} | ||
}, | ||
"EnvironmentRole": { | ||
"type": "object", | ||
"properties": { | ||
"roleName": { | ||
"type": "string", | ||
"readOnly": true | ||
}, | ||
"description": { | ||
"type": "string", | ||
"readOnly": true | ||
} | ||
} | ||
} | ||
}, | ||
"parameters": { | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
presumably this should be passing
parsingModel
in when looping around? else we'll potentially be passingtrue
when thefalse
thing:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should always pass a
true
here, because the Schame represented bytopLevelObject
is what the*objectName
want. that means we can usetopLevelObject
to parse model of*objectName
. also as other two calls below, the inner/inheritedModel is corresponding to the modelName, and they should always pass atrue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Importing the full set of Services there's no difference with this logic, but threading this through causes:
So this looks fine 👍