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

Make TypeResolverBase.RemoveNullability virtual for extendability #1547

Closed
SamuelBerger opened this issue Aug 7, 2022 · 1 comment
Closed

Comments

@SamuelBerger
Copy link
Contributor

We use JsonSchema.OneOf with multiple JsonSchemas and generate TypeScript union types out of it.

The current implementation of TypeResolverBase.RemoveNullability "destroys" the JsonSchema.OneOf collection to the first non nullable item:

        /// <summary>Removes a nullable oneOf reference if available.</summary>
        /// <param name="schema">The schema.</param>
        /// <returns>The actually resolvable schema</returns>
        public JsonSchema RemoveNullability(JsonSchema schema)
        {
            // TODO: Method on JsonSchema4?
            return schema.OneOf.FirstOrDefault(o => !o.IsNullable(SchemaType.JsonSchema)) ?? schema;
        }

So we end up with the first non nullable item instead of the whole OneOf collection with multiple non nullable items.

Currently we use an ugly hack in GetOrGenerateTypeName to circumvent the problem:

  • setting all items to IsNullableRaw = true (so RemoveNullability does not replace our OneOf schema with the first item)
  • call the real GetOrGenerateTypeName
  • setting all items back to IsNullableRaw = null
    public class MyTypeScriptTypeResolver : TypeScriptTypeResolver
    {
        public MyTypeScriptTypeResolver(TypeScriptGeneratorSettings settings) : base(settings) { }

        public override string GetOrGenerateTypeName(JsonSchema schema, string typeNameHint) {
            if (schema.OneOf.Count > 1) {
                // hack to escape the RemoveNullability destroying our discriminated union
                foreach (var item in schema.OneOf) {
                    item.IsNullableRaw = true;
                }

                var name = base.GetOrGenerateTypeName(schema, typeNameHint);

                foreach (var item in schema.OneOf) {
                    item.IsNullableRaw = null;
                }

                return name;
            }

            return base.GetOrGenerateTypeName(schema, typeNameHint);
        }
    }  

Making TypeResolverBase.RemoveNullability virtual gives the possibility to override the current behaviour.

SamuelBerger added a commit to SamuelBerger/NJsonSchema that referenced this issue Aug 7, 2022
…lity

This allows to implement different logic e.g. treat JsonSchema.OneOf
to have multiple (non nullable) items instead of zero or one.
SamuelBerger added a commit to SamuelBerger/NJsonSchema that referenced this issue Aug 7, 2022
…lity

This allows to implement different logic e.g. treat JsonSchema.OneOf
to have multiple (non nullable) items instead of zero or one.
SamuelBerger added a commit to SamuelBerger/NJsonSchema that referenced this issue Aug 7, 2022
This allows to implement different logic e.g. treat JsonSchema.OneOf
to have multiple (non nullable) items instead of zero or one.
RicoSuter pushed a commit that referenced this issue Aug 15, 2022
This allows to implement different logic e.g. treat JsonSchema.OneOf
to have multiple (non nullable) items instead of zero or one.
@glopesdev
Copy link

@SamuelBerger @RicoSuter it seems like #1548 resolved this issue but it wasn't closed somehow.

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

No branches or pull requests

2 participants