Skip to content

Add a reserved namespaces deny list to restrict trait-codegen from using smithy specific namespaces.#2709

Merged
yasmewad merged 1 commit intomainfrom
trait-codegen-reserve-namespace
Jul 23, 2025
Merged

Add a reserved namespaces deny list to restrict trait-codegen from using smithy specific namespaces.#2709
yasmewad merged 1 commit intomainfrom
trait-codegen-reserve-namespace

Conversation

@yasmewad
Copy link
Contributor

Background

What do these changes do?
Adds validation to prevent trait code generation on smithy keyword itself or reserved Smithy namespaces ( smithy.api, smithy.waiters, smithy.test) and their sub-namespaces but we still want to allow namespaces like smithy.foo or smithy.rules .

This change adds a RESERVED_NAMESPACES set to act as a deny list for future extensibility.

For instance:

smithy -> not allowed
smithy.api , smithy.waiters , and smithy.test -> not allowed
smithy.foo, smithyfoo.bar, -> allowed

Why are they important?
Prevents namespace conflicts with core Smithy functionality and protects framework integrity by blocking user code generation on reserved namespaces.

Testing

How did you test these changes?
Added comprehensive unit tests covering reserved namespace validation, case-insensitive matching, edge cases, and legitimate namespace acceptance.

Links

• No related issues


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@yasmewad yasmewad requested a review from a team as a code owner July 22, 2025 06:01
@yasmewad yasmewad requested a review from joewyz July 22, 2025 06:01
@SmithyUnstableApi
public final class TraitCodegenSettings {
private static final String SMITHY_KEYWORD = "smithy";
private static final Set<String> RESERVED_NAMESPACES = SetUtils.of("smithy.api", "smithy.waiters", "smithy.test");
Copy link
Contributor

@sugmanue sugmanue Jul 22, 2025

Choose a reason for hiding this comment

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

You are missing some, the full list as of now is:

smithy.api
smithy.framework
smithy.mqtt
smithy.openapi
smithy.protocols
smithy.rules
smithy.test
smithy.waiters

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack. Are these from just the github search? Or some other mechanism you used?

Copy link
Contributor

Choose a reason for hiding this comment

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

A shell script:

find . -name \*.smithy | xargs egrep '^namespace ' | grep ' smithy.' | cut -d: -f2  | grep -v ' test' | grep -v example | sort | uniq | cut -d ' ' -f2 | cut -d '.' -f1,2 | uniq

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the namespace list.

@SmithyUnstableApi
public final class TraitCodegenSettings {
private static final String SMITHY_KEYWORD = "smithy";
private static final Set<String> RESERVED_NAMESPACES = SetUtils.of("smithy.api", "smithy.waiters", "smithy.test");
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs several other sub-namespaces: .mqtt, .openapi, .protocols, .rules, .framework

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the namespace list.

@yasmewad yasmewad force-pushed the trait-codegen-reserve-namespace branch from 3e8765f to 757fc0d Compare July 22, 2025 19:10
* @return true if the namespace is reserved, false otherwise
*/
private static boolean isReservedNamespace(String namespace) {
String namespaceLowerCase = namespace.toLowerCase(Locale.ROOT);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's remove this conversion, namespaces are case sensitive.

Copy link
Contributor

Choose a reason for hiding this comment

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

Confirmed offline that this needs to be case sensitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, while namespaces are case-sensitive, the rationale of this was anyone should be forbidden from creating a trait with SMITHY or SMITHY.API or other versions of it as it conflicts with our reserved namespaces.

I am leaning towards blocking those case-insensitively. @mtdowling and I discussed, the reasoning for case-insensitive checking is that shapes are case-insensitive.

FWIW, for future record putting this here from our docs :

While shape ID references within the semantic model are case-sensitive, no two shapes in the semantic model can have the same case-insensitive shape ID. This restriction makes it easier to use Smithy models for code generation in programming languages that do not support case-sensitive identifiers or that perform some kind of normalization on generated identifiers (for example, a Python code generator might convert all member names to lower snake case). To illustrate, com.Foo#baz and com.foo#BAZ are not allowed in the same semantic model. This restriction also extends to member names: com.foo#Baz$bar and com.foo#Baz$BAR are in conflict.

@yasmewad yasmewad requested review from yefrig and removed request for joewyz July 22, 2025 23:07
@yasmewad yasmewad merged commit be65518 into main Jul 23, 2025
16 of 17 checks passed
@yasmewad yasmewad deleted the trait-codegen-reserve-namespace branch July 23, 2025 15:38
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.

4 participants