-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Clean up ApiGenerator #3755
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
Merged
Merged
Clean up ApiGenerator #3755
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
09d0240
Update code configuration for low level client to only generate metho…
Mpdreamz 5966447
Fixed locations to be nix friendly
Mpdreamz 2825868
Removed alternative HTTP method named methods in the low level client
Mpdreamz bdf65b2
progress commit
Mpdreamz cdb327c
Simplified CsharpMethodsWithQueryString
Mpdreamz 6de0d9b
progress commit
Mpdreamz 8271368
progress commit
Mpdreamz 9c7518d
can now use partials/include this should start cleaning up the cshtml…
Mpdreamz 6a4dfe4
Able to introduce partial methods now
Mpdreamz a21333b
Add partial views
Mpdreamz a078b9e
RequestDescriptorBase separate view
Mpdreamz 628f30c
moved descriptors to partials (initial stab)
Mpdreamz 900d0c7
reindent generated code automatically
Mpdreamz cf31801
Split name and namespace
Mpdreamz 12fc139
Request/Descriptors are no longer backed by CsharpMethods
Mpdreamz 28d56b4
start cleaning up CsharpMethod
Mpdreamz b94fcbc
PatchMethod on overrides is gone,
Mpdreamz a248588
Cleaned up code configuration a tad
Mpdreamz 27c4239
deduplication is now gone, marked aliases that were duplicate as depr…
Mpdreamz 38d8a1e
In case of GET, OTHER prefer OTHER
Mpdreamz 8e5114f
rebased against 7.x and reworked replace files into patch files
Mpdreamz fe48451
skip params also on requestparameters and prefer plural source_includes
Mpdreamz 5bf421f
reapply fix from #3700 to make sure virtual paths as root are support…
Mpdreamz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using ApiGenerator.Domain; | ||
using ApiGenerator.Overrides.Descriptors; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace ApiGenerator | ||
{ | ||
public static class ApiEndpointFactory | ||
{ | ||
public static ApiEndpoint FromFile(string jsonFile) | ||
{ | ||
var officialJsonSpec = JObject.Parse(File.ReadAllText(jsonFile)); | ||
PatchOfficialSpec(officialJsonSpec, jsonFile); | ||
var (name, endpoint) = officialJsonSpec.ToObject<Dictionary<string, ApiEndpoint>>().First(); | ||
|
||
endpoint.FileName = Path.GetFileName(jsonFile); | ||
endpoint.Name = name; | ||
var tokens = name.Split("."); | ||
|
||
endpoint.MethodName = tokens.Last(); | ||
if (tokens.Length > 1) | ||
endpoint.Namespace = tokens[0]; | ||
//todo side effect | ||
endpoint.CsharpNames = new CsharpNames(name, endpoint.MethodName, endpoint.Namespace); | ||
endpoint.Url.CsharpNames = endpoint.CsharpNames; | ||
|
||
LoadOverridesOnEndpoint(endpoint); | ||
PatchRequestParameters(endpoint); | ||
|
||
EnforceRequiredOnParts(jsonFile, endpoint.Url); | ||
return endpoint; | ||
} | ||
|
||
/// <summary> | ||
/// This makes sure required is configured correctly by inspecting the paths. | ||
/// Will emit a warning if the spec file got this wrong | ||
/// </summary> | ||
private static void EnforceRequiredOnParts(string jsonFile, UrlInformation url) | ||
{ | ||
if (url.IsPartless) return; | ||
foreach (var part in url.Parts) | ||
{ | ||
var required = url.Paths.All(p => p.Path.Contains($"{{{part.Name}}}")); | ||
if (part.Required != required) | ||
ApiGenerator.Warnings.Add($"{jsonFile} has part: {part.Name} listed as {part.Required} but should be {required}"); | ||
part.Required = required; | ||
} | ||
} | ||
|
||
private static void LoadOverridesOnEndpoint(ApiEndpoint endpoint) | ||
{ | ||
var method = endpoint.CsharpNames.MethodName; | ||
if (CodeConfiguration.ApiNameMapping.TryGetValue(endpoint.Name, out var mapsApiMethodName)) | ||
method = mapsApiMethodName; | ||
|
||
var typeName = "ApiGenerator.Overrides.Endpoints." + method + "Overrides"; | ||
var type = GeneratorLocations.Assembly.GetType(typeName); | ||
if (type != null && Activator.CreateInstance(type) is IEndpointOverrides overrides) | ||
endpoint.Overrides = overrides; | ||
} | ||
|
||
private static void PatchRequestParameters(ApiEndpoint endpoint) | ||
{ | ||
var newParams = ApiQueryParametersPatcher.Patch(endpoint.Name, endpoint.Url.Params, endpoint.Overrides); | ||
endpoint.Url.Params = newParams; | ||
} | ||
|
||
/// <summary> | ||
/// Finds a patch file in patches and union merges this with the official spec. | ||
/// This allows us to check in tweaks should breaking changes occur in the spec before we catch them | ||
/// </summary> | ||
private static void PatchOfficialSpec(JObject original, string jsonFile) | ||
{ | ||
var directory = Path.GetDirectoryName(jsonFile); | ||
var patchFile = Path.Combine(directory,"..", "_Patches", Path.GetFileNameWithoutExtension(jsonFile)) + ".patch.json"; | ||
if (!File.Exists(patchFile)) return; | ||
|
||
var patchedJson = JObject.Parse(File.ReadAllText(patchFile)); | ||
|
||
var pathsOverride = patchedJson.SelectToken("*.url.paths"); | ||
|
||
original.Merge(patchedJson, new JsonMergeSettings | ||
{ | ||
MergeArrayHandling = MergeArrayHandling.Union | ||
}); | ||
|
||
if (pathsOverride != null) original.SelectToken("*.url.paths").Replace(pathsOverride); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 simplifies the |
||
} | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray TODO?