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

Fix schema name that has end with space for redoc ui and elements ui #856

Merged
merged 3 commits into from
Jan 10, 2024
Merged

Fix schema name that has end with space for redoc ui and elements ui #856

merged 3 commits into from
Jan 10, 2024

Conversation

duchuyvp
Copy link
Contributor

I strip component_id before register a new schema when to prevent its name has some bad space, generated spec has schama name that end with space like 'Schema name ' doesn't work with redoc ui (by ReDoc) and elements ui (by Stoplight)

@lafrech
Copy link
Member

lafrech commented Aug 29, 2023

AFAIU, the space in the schema name a user error that may be a bit tricky to spot but that should be easily fixed.

I may be convinced otherwise but I don't think apispec should engage in this. We can't workaround all typos.

@duchuyvp
Copy link
Contributor Author

duchuyvp commented Aug 29, 2023

You have point out that maybe it's a user error, I did try named a schema like "Pet In Schema" and the error occur, cause after remove postfix "Schema" in that name, it's stay "Pet In " and this does not look like user error or typo. Even I can simply fix this by replace the name with "Pet In" or "Pet InSchema" but the second way may looks like typo error.

@lafrech
Copy link
Member

lafrech commented Aug 29, 2023

Good point. We could improve the schema name generator to strip spaces there.

How do you create the schema?

Schemas are generally created as classes so you can't have spaces in their names.

class PetSchema(Schema):
    ...

@duchuyvp
Copy link
Contributor Author

duchuyvp commented Aug 30, 2023

Sorry about delay.

I used from_dict to generate schema.

from marshmallow.fields import String

pet_schema = Schema.from_dict({"name": String()}, name="Pet In Schema")

@lafrech
Copy link
Member

lafrech commented Aug 30, 2023

OK.

Users probably shouldn't give names with spaces in the first place but since it is not forbidden, I have no objection to stripping the generated name in the default name resolver:

def resolver(schema: type[Schema]) -> str:
"""Default schema name resolver function that strips 'Schema' from the end of the class name."""
schema_cls = resolve_schema_cls(schema)
name = schema_cls.__name__
if name.endswith("Schema"):
return name[:-6] or name
return name
.

@duchuyvp
Copy link
Contributor Author

Thanks for your consider. Would the branch be merge?

@duchuyvp
Copy link
Contributor Author

pre-commit.ci run

@sloria
Copy link
Member

sloria commented Jan 10, 2024

Made some minor fixups so it's ready to merge. Thanks for the PR!

@sloria sloria enabled auto-merge (squash) January 10, 2024 03:51
@sloria sloria merged commit 8af6453 into marshmallow-code:dev Jan 10, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants