Azure / autorest

OpenAPI (f.k.a Swagger) Specification code generator. Supports C#, PowerShell, Go, Java, Node.js, TypeScript, Python
MIT License
4.54k stars 728 forks source link

Express resource hierarchy #4955

Open pshao25 opened 2 months ago

pshao25 commented 2 months ago

See this swagger as an example, currently the generated TypeSpec will throw error when compile:

Path contains parameter projectName but wasn't found in given parameters

Solution 1: we could see from it that Project might be the parent resource of ProjectDeployment.

@resource("projects")
model Project {
  @key("projectName")
  name: string;
}

@resource("deployments")
@parentResource(Project)
model ProjectDeployment {
  @key
  deploymentName: string;
}

@route("authoring/analyze-text/")
interface Test {
  listDeployments is Azure.Core.ResourceList<
    ProjectDeployment,
    Traits    
  >;
}

Solution 2: but we actually don't know the parent information from swagger. So we add the projectName directly to Traits.

@trait
model Traits {
  @traitContext(TraitContext.List)
  queryParams: {
    @traitLocation(TraitLocation.Parameters)
    parameters: {
        @path
        projectName: string;
      };
  }
};

Both of these two solutions have a problem: it needs to promote the url prefix to interface. If the swagger contains more than one prefixes, we have to generate multiple interfaces, which leads to multiple clients in the end.