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

Check bool examples, don't crash on nonexistent examples. #58

Merged
merged 6 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions semantic-conventions/semconv.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,14 @@
"description": "additional notes to the attribute. It defaults to an empty string."
},
"examples": {
"$ref": "#/definitions/ValueType",
"description": "sequence/dictionary of example values for the attribute. They are optional for boolean and enum attributes. Example values must be of the same type of the attribute. If only a single example is provided, it can directly be reported without encapsulating it into a sequence/dictionary."
"anyOf": [
{ "$ref": "#/definitions/ValueType" },
{
"type": "array",
"items": { "$ref": "#/definitions/ValueType" }
}
],
"description": "sequence/dictionary of example values for the attribute. They are optional for boolean, int, double, and enum attributes. Example values must be of the same type of the attribute. If only a single example is provided, it can directly be reported without encapsulating it into a sequence/dictionary."
},
"deprecated": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,25 +250,25 @@ def parse_id(attribute):
position = attribute.lc.data[list(attribute)[0]]
msg = "Non array examples for {} are not allowed".format(attr_type)
raise ValidationError.from_yaml_pos(position, msg)
if (
(is_simple_type or isinstance(attr_type, EnumAttributeType))
and not isinstance(examples, CommentedSeq)
and examples is not None
):
if not isinstance(examples, CommentedSeq) and examples is not None:
# TODO: If validation fails later, this will crash when trying to access positio data
Oberon00 marked this conversation as resolved.
Show resolved Hide resolved
# since a list, contratry to a CommentedSeq, does not have position data
Oberon00 marked this conversation as resolved.
Show resolved Hide resolved
examples = [examples]
if is_simple_type and attr_type not in [
if is_simple_type and attr_type not in (
"boolean",
"boolean[]",
"int",
"int[]",
"double",
"double[]",
]:
if examples is None or (len(examples) == 0):
):
if not examples:
position = attribute.lc.data[list(attribute)[0]]
msg = "Empty examples for {} are not allowed".format(attr_type)
raise ValidationError.from_yaml_pos(position, msg)
if is_simple_type and attr_type not in ["boolean", "boolean[]"]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thisthat Any idea why this was originally excluded? To support more boolean values like "yes" instead of "true"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we excluded boolean attributes from having an example because it is not very useful to have true/false there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know they are not required to have examples (though before this patch, non-array bools without examples would have crashed the generator). But in case they do have examples (which is allowed), the examples should still be checked for whether they have the right type.


# TODO: Implement type check for enum examples or forbid them
if examples is not None and is_simple_type:
AttributeType.check_examples_type(attr_type, examples, zlass)
return attr_type, str(brief), examples

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "0.4.1"
__version__ = "0.4.2"
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ groups:
- id: host.port
type: int
brief: 'Like `net.peer.port` but for the host port.'
examples: 35555
- id: host.name
type: string
brief: 'Local hostname or similar, see note below.'
Expand All @@ -83,4 +82,4 @@ groups:
Scopes or granted authorities the client currently possesses extracted from token
or application security context. The value would come from the scope associated
with an OAuth 2.0 Access Token or an attribute value in a SAML 2.0 Assertion.
examples: 'read:message, write:files'
examples: 'read:message, write:files'
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ groups:
type: boolean
required: always
brief: 'test'
examples: [true, false, yes]
examples: [true, false, true]
arminru marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ groups:
type: boolean[]
required: always
brief: 'test'
examples: [[true,false], [true, 1]]
examples: [[true,false], [true, false]]