AvoidAnonymousTypes
October 10, 2022 · View on GitHub
Category
SDK Error
Applies to
ARM and Data plane OpenAPI(swagger) specs
Output Message
Inline/anonymous models must not be used, instead define a schema with a model name in the "definitions" section and refer to it. This allows operations to share the models.
Description
This rule appears when you define a model type inline, rather than in the definitions section. If the model represents the same type as another parameter in a different operation, then it becomes impossible to reuse that same class for both operations.
Why the rule is important
Anonymous parameters will be autogenerated as non-descriptive parameters which the client will not be able to share across operations or provide good documentation for, thereby resulting in poor user experience.
How to fix the violation
Move the schema to the definitions section and reference it using $ref.
Impact on generated code
Before
Before
Spec:
…
"parameters":[
{
"name": "foo",
"in": "body",
"schema": {
"type": "object",
"properties": {
…
}
}
}
]
…
Generated code:
public class FooParameter1 {
…
}
After
After
Spec:
…
"parameters": [
{
"name": "foo",
"in": "body",
"schema": {
"$ref": "#/definition/FooCreationSettings"
}
}
],
…
"definitions": {
"FooCreationSettings": {
"type": "object",
"properties": {
…
}
}
}
…
Generated code:
public class FooCreationSettings {
…
}
Examples
N/A.