Skip to content

Commit

Permalink
🐛 fix reserve word validate
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Oct 21, 2022
1 parent dc0cfcc commit b1a91a7
Show file tree
Hide file tree
Showing 5 changed files with 36,245 additions and 400 deletions.
82 changes: 53 additions & 29 deletions codegen/parser/schemas/model_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
DateSchema,
EnumSchema,
FileSchema,
ListSchema,
NoneSchema,
SchemaData,
FloatSchema,
Expand Down Expand Up @@ -109,11 +110,13 @@ def _is_string_subset(
def _is_model_merge(
source: Source, name: str, prefix: str, first: SchemaData, second: SchemaData
) -> Optional[ModelSchema]:
if isinstance(first, ModelSchema) and isinstance(second, ModelSchema):
properties = {prop.name: prop for prop in first.properties}
if (first_model := _find_schema(first, ModelSchema)) and (
second_model := _find_schema(second, ModelSchema)
):
properties = {prop.name: prop for prop in first_model.properties}
class_name = build_class_name(prefix)

for prop in second.properties:
for prop in second_model.properties:
if prop.name in properties:
# try merge
try:
Expand All @@ -133,12 +136,42 @@ def _is_model_merge(
schema = ModelSchema(
class_name=class_name,
properties=list(properties.values()),
allow_extra=first.allow_extra and second.allow_extra,
allow_extra=first_model.allow_extra and second_model.allow_extra,
)
add_schema((source / "allof" / "merged" / name).uri, schema)
return schema


def _is_list_merge(
source: Source, name: str, prefix: str, first: SchemaData, second: SchemaData
) -> Optional[ListSchema]:
if isinstance(first, ListSchema) and isinstance(second, ListSchema):
return ListSchema(
title=first.title,
description=first.description,
default=first.default,
examples=first.examples,
item_schema=_merge_schema(
source, name, prefix, first.item_schema, second.item_schema
),
)


def _merge_schema(
source: Source, name: str, prefix: str, first: SchemaData, second: SchemaData
):
if schema := (
_is_union_subset(first, second)
or _is_string_subset(first, second)
or _is_string_subset(second, first)
or _is_enum_subset(first, second)
or _is_model_merge(source, name, prefix, first, second)
or _is_list_merge(source, name, prefix, first, second)
):
return schema
raise RuntimeError(f"Cannot merge schema {first!r} {second!r}")


def _merge_property(
source: Source, first: Property, second: Property, prefix: str
) -> Property:
Expand All @@ -148,31 +181,23 @@ def _merge_property(
required = first.required or second.required
nullable = _is_nullable(first.schema_data) and _is_nullable(second.schema_data)

if schema := (
_is_union_subset(first.schema_data, second.schema_data)
or _is_string_subset(first.schema_data, second.schema_data)
or _is_string_subset(second.schema_data, first.schema_data)
or _is_enum_subset(first.schema_data, second.schema_data)
or _is_model_merge(
source, first.name, prefix, first.schema_data, second.schema_data
)
):
if nullable:
schema = UnionSchema(
title=schema.title,
description=schema.description,
default=schema.default,
examples=schema.examples,
schemas=[schema, NoneSchema()],
)
return Property(
name=second.name,
prop_name=second.prop_name,
required=required,
schema_data=schema,
schema = _merge_schema(
source, first.name, prefix, first.schema_data, second.schema_data
)
if nullable:
schema = UnionSchema(
title=schema.title,
description=schema.description,
default=schema.default,
examples=schema.examples,
schemas=[schema, NoneSchema()],
)

raise RuntimeError(f"Cannot merge property {first!r} {second!r}")
return Property(
name=second.name,
prop_name=second.prop_name,
required=required,
schema_data=schema,
)


def _process_properties(
Expand Down Expand Up @@ -247,7 +272,6 @@ def _add_if_no_conflict(prop: Property):
schema_data=prop_schema,
)
)

return list(properties.values())


Expand Down
4 changes: 3 additions & 1 deletion codegen/parser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def split_words(value: str) -> List[str]:
return re.findall(rf"[^{DELIMITERS}]+", value)


RESERVED_WORDS = (set(dir(builtins)) | {"self", "true", "false", "datetime"}) - {
RESERVED_WORDS = (
set(dir(builtins)) | {"self", "true", "false", "datetime", "validate"}
) - {
"type",
"id",
}
Expand Down
4 changes: 2 additions & 2 deletions githubkit/rest/code_scanning.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ def upload_sarif(
checkout_uri: Union[Unset, str] = UNSET,
started_at: Union[Unset, datetime] = UNSET,
tool_name: Union[Unset, str] = UNSET,
validate: Union[Unset, bool] = UNSET,
validate_: Union[Unset, bool] = UNSET,
) -> "Response[CodeScanningSarifsReceipt]":
...

Expand Down Expand Up @@ -802,7 +802,7 @@ async def async_upload_sarif(
checkout_uri: Union[Unset, str] = UNSET,
started_at: Union[Unset, datetime] = UNSET,
tool_name: Union[Unset, str] = UNSET,
validate: Union[Unset, bool] = UNSET,
validate_: Union[Unset, bool] = UNSET,
) -> "Response[CodeScanningSarifsReceipt]":
...

Expand Down
Loading

0 comments on commit b1a91a7

Please sign in to comment.