geniespec 0.1.2.dev11__tar.gz → 0.1.2.dev13__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (22) hide show
  1. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/PKG-INFO +6 -2
  2. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/README.md +5 -1
  3. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/pyproject.toml +1 -1
  4. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/pyproject.toml.orig +1 -1
  5. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/api_schema.yaml +2 -0
  6. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/genie.py +24 -14
  7. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/languages.py +2 -2
  8. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/renderable_types.py +1 -0
  9. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/types.py +1 -0
  10. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/__init__.py +0 -0
  11. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/cli.py +0 -0
  12. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/python-server/{service}/__init__.py.j2 +0 -0
  13. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/python-server/{service}/v{version}/__init__.py.j2 +0 -0
  14. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/python-server/{service}/v{version}/api.py.j2 +0 -0
  15. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/python-server/{service}/v{version}/endpoints.py.j2 +0 -0
  16. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/python-server/{service}/v{version}/exceptions.py.j2 +0 -0
  17. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/python-server/{service}/v{version}/internal.py.j2 +0 -0
  18. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/python-server/{service}/v{version}/resources.py.j2 +0 -0
  19. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/typescript-client/{service}/v{version}/client.ts.j2 +0 -0
  20. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/typescript-client/{service}/v{version}/endpoints.ts.j2 +0 -0
  21. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/typescript-client/{service}/v{version}/index.ts.j2 +0 -0
  22. {geniespec-0.1.2.dev11 → geniespec-0.1.2.dev13}/src/geniespec/templates/typescript-client/{service}/v{version}/resources.ts.j2 +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: geniespec
3
- Version: 0.1.2.dev11
3
+ Version: 0.1.2.dev13
4
4
  Summary: Generate API code from an api.yaml spec
5
5
  License-Expression: MIT
6
6
  Requires-Dist: jinja2>=3.1.6
@@ -117,7 +117,11 @@ resources:
117
117
  extensions: {deprecated: true}
118
118
  ```
119
119
 
120
- `python-server` passes the transition fields to kiba-core's `route`, whose `OpenApiSchemaGenerator` documents every route with an `operationId` (and skips the rest); `extensions` reach `OpenApiExtension`s as `metadata['extensions']`. Field descriptions become pydantic `Field(description=...)`, so they show up in the request and response schemas. Each `operationId` must be unique, so a transition that generates more than one route (for example `GET` on a resource with unique attributes) can't set one. `typescript-client` ignores these fields.
120
+ `python-server` passes the transition fields to kiba-core's `route`, whose `OpenApiSchemaGenerator` documents every route with an `operationId` (and skips the rest); `extensions` reach `OpenApiExtension`s as `metadata['extensions']`. Field descriptions become pydantic `Field(description=...)`, so they show up in the request and response schemas. Each `operationId` must be unique, so a transition that generates more than one route (for example a `GET` on a resource that is also `accessibleFrom` root) can't set one. `typescript-client` ignores these fields.
121
+
122
+ ## Unique attributes
123
+
124
+ Resources are addressed by their primary attribute. A `GET` transition also adds a lookup for each `isUnique` attribute (`/assets/address/{address}`), and, on a child resource, for each `isUniqueInParent` attribute: one that is only unique within its parent, like an asset's address on a chain. Those lookups only exist through the parent (`/chains/{chainId}/assets/address/{address}`), never from root or another ancestor. Lookups share the `GET` transition's documentation fields, with `By<Attribute>` added to its `operationId` (`getAsset` → `getAssetByAddress`).
121
125
 
122
126
  ## Overrides
123
127
 
@@ -103,7 +103,11 @@ resources:
103
103
  extensions: {deprecated: true}
104
104
  ```
105
105
 
106
- `python-server` passes the transition fields to kiba-core's `route`, whose `OpenApiSchemaGenerator` documents every route with an `operationId` (and skips the rest); `extensions` reach `OpenApiExtension`s as `metadata['extensions']`. Field descriptions become pydantic `Field(description=...)`, so they show up in the request and response schemas. Each `operationId` must be unique, so a transition that generates more than one route (for example `GET` on a resource with unique attributes) can't set one. `typescript-client` ignores these fields.
106
+ `python-server` passes the transition fields to kiba-core's `route`, whose `OpenApiSchemaGenerator` documents every route with an `operationId` (and skips the rest); `extensions` reach `OpenApiExtension`s as `metadata['extensions']`. Field descriptions become pydantic `Field(description=...)`, so they show up in the request and response schemas. Each `operationId` must be unique, so a transition that generates more than one route (for example a `GET` on a resource that is also `accessibleFrom` root) can't set one. `typescript-client` ignores these fields.
107
+
108
+ ## Unique attributes
109
+
110
+ Resources are addressed by their primary attribute. A `GET` transition also adds a lookup for each `isUnique` attribute (`/assets/address/{address}`), and, on a child resource, for each `isUniqueInParent` attribute: one that is only unique within its parent, like an asset's address on a chain. Those lookups only exist through the parent (`/chains/{chainId}/assets/address/{address}`), never from root or another ancestor. Lookups share the `GET` transition's documentation fields, with `By<Attribute>` added to its `operationId` (`getAsset` → `getAssetByAddress`).
107
111
 
108
112
  ## Overrides
109
113
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "geniespec"
3
- version = "0.1.2.dev11"
3
+ version = "0.1.2.dev13"
4
4
  description = "Generate API code from an api.yaml spec"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "geniespec"
3
- version = "0.1.2.dev11"
3
+ version = "0.1.2.dev13"
4
4
  description = "Generate API code from an api.yaml spec"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -148,6 +148,8 @@ definitions:
148
148
  type: boolean
149
149
  isUnique:
150
150
  type: boolean
151
+ isUniqueInParent:
152
+ type: boolean
151
153
  shouldDisableGetEndpoint:
152
154
  type: boolean
153
155
  isUnfilterable:
@@ -233,6 +233,7 @@ class Renderer:
233
233
  ),
234
234
  defaultValue=attribute.defaultValue,
235
235
  isUnique=attribute.isUnique,
236
+ isUniqueInParent=attribute.isUniqueInParent,
236
237
  isPrimary=attribute.isPrimary,
237
238
  isUpdatable=attribute.isUpdatable,
238
239
  isAccessibleIndependently=attribute.isAccessibleIndependently,
@@ -465,6 +466,9 @@ class Renderer:
465
466
  resourceErrors.append(f'{fullName} can only be accessible from root or an ancestor, not {anchorName}')
466
467
  parentPrimaryAttribute = resourcesByName[resource.parentResource].primaryAttribute if resource.parentResource in resourcesByName else None
467
468
  isOneToOne = parentPrimaryAttribute is not None and resource.primaryAttribute is not None and resource.primaryAttribute.name == parentPrimaryAttribute.name
469
+ resourceErrors += [f'{attribute.name} on {fullName} cannot be both isUniqueInParent and isPrimary or isUnique' for attribute in resource.attributes if attribute.isUniqueInParent and (attribute.isPrimary or attribute.isUnique)]
470
+ if any(attribute.isUniqueInParent for attribute in resource.attributes) and (resource.parentResource is None or isOneToOne):
471
+ resourceErrors.append(f'{fullName} can only have isUniqueInParent attributes when it is a (not one-to-one) child resource')
468
472
  expectedPrimaryName = f'{RenderableString.from_pascal_case_string(string=fullName).kebabCaseString}-id'
469
473
  if resource.transitions and resource.primaryAttribute is not None and not isOneToOne and resource.primaryAttribute.name != expectedPrimaryName:
470
474
  resourceErrors.append(f'Primary attribute of {fullName} must be {expectedPrimaryName}, found {resource.primaryAttribute.name}')
@@ -525,6 +529,7 @@ class Renderer:
525
529
  authentication = transition.authentication or api.defaultAuthentication
526
530
  rateLimit = transition.rateLimit or api.defaultRateLimit
527
531
  operationCount = len(renderableApi.operations)
532
+ lookupOperations: list[tuple[RenderableOperation, Attribute]] = []
528
533
  method: RenderableMethod = self._transition_to_renderable_method(transition=transition)
529
534
  if transition.isIndirectAction and transition.action.upper() in {'DELETE', 'CREATE', 'UPDATE', 'GET', 'LIST'}:
530
535
  raise SemanticValidationException(message=f'Only custom transitions can be indirect actions: {transition.action}')
@@ -557,21 +562,23 @@ class Renderer:
557
562
  renderableApi.operations.append(RenderableOperation(method=method, endpoint=endpoint, query=None, body=None, responseFields=[returnValue], authentication=authentication, rateLimit=rateLimit))
558
563
  endpointsAccessibleWithUnique = [endpoint for endpoint in endpoints.collectionEndpoints if isinstance(endpoint.components[-1], ResourceEndpointComponent)]
559
564
  uniqueAttributes = [attribute for attribute in resource.attributes if attribute.isUnique and not attribute.isPrimary and not attribute.shouldDisableGetEndpoint and not attribute.isAccessibleIndependently]
560
- for endpoint in endpointsAccessibleWithUnique:
565
+ uniqueInParentAttributes = [attribute for attribute in resource.attributes if attribute.isUniqueInParent and not attribute.shouldDisableGetEndpoint and not attribute.isAccessibleIndependently]
566
+ lookups = [(endpoint, attribute) for endpoint in endpointsAccessibleWithUnique for attribute in uniqueAttributes]
567
+ lookups += [(endpoint, attribute) for endpoint in self._get_canonical_endpoints(processableResource=processableResource).collectionEndpoints for attribute in uniqueInParentAttributes]
568
+ for endpoint, attribute in lookups:
561
569
  lastComponent = typing.cast(ResourceEndpointComponent, endpoint.components[-1])
562
- for attribute in uniqueAttributes:
563
- getByUniqueEndpoint = RenderableEndpoint(
564
- components=[
565
- *endpoint.components[:-1],
566
- ResourceEndpointComponent(
567
- resource=lastComponent.resource,
568
- attribute=self._attribute_to_renderable_attribute(attribute=attribute, isNullable=False, knownTypeNames=knownTypeNames),
569
- relativeSingleName=lastComponent.relativeSingleName,
570
- relativeCollectionName=lastComponent.relativeCollectionName,
571
- ),
572
- ]
573
- )
574
- renderableApi.operations.append(RenderableOperation(method=method, endpoint=getByUniqueEndpoint, query=None, body=None, responseFields=[returnValue], authentication=authentication, rateLimit=rateLimit))
570
+ getByUniqueEndpoint = RenderableEndpoint(
571
+ components=[
572
+ *endpoint.components[:-1],
573
+ ResourceEndpointComponent(
574
+ resource=lastComponent.resource,
575
+ attribute=self._attribute_to_renderable_attribute(attribute=attribute, isNullable=False, knownTypeNames=knownTypeNames),
576
+ relativeSingleName=lastComponent.relativeSingleName,
577
+ relativeCollectionName=lastComponent.relativeCollectionName,
578
+ ),
579
+ ]
580
+ )
581
+ lookupOperations.append((RenderableOperation(method=method, endpoint=getByUniqueEndpoint, query=None, body=None, responseFields=[returnValue], authentication=authentication, rateLimit=rateLimit), attribute))
575
582
  elif transition.action.upper() == 'LIST':
576
583
  returnValue = RenderableField(name=processableResource.renderableResource.collectionName, fieldType=RenderableType(rawType='class', className=processableResource.renderableResource.singleName, isNullable=False, isList=True))
577
584
  query = [self._request_field_to_renderable_parameter(requestField=requestField, knownTypeNames=knownTypeNames) for requestField in transition.requestFields]
@@ -589,6 +596,9 @@ class Renderer:
589
596
  renderableApi.operations.append(self._action_operation(transition=transition, endpoint=customEndpoint, knownTypeNames=knownTypeNames, defaultAuthentication=api.defaultAuthentication, defaultRateLimit=api.defaultRateLimit, isIndirectAction=transition.isIndirectAction))
590
597
  isStandard = transition.action.upper() in {'CREATE', 'UPDATE', 'GET', 'LIST'}
591
598
  renderableApi.operations[operationCount:] = [self._with_transition_fields(operation=operation, transition=transition, isStandard=isStandard) for operation in renderableApi.operations[operationCount:]]
599
+ for operation, attribute in lookupOperations:
600
+ lookupOperationId = f'{transition.operationId}By{RenderableString.from_kebab_case_string(string=attribute.name).pascalCaseString}' if transition.operationId is not None else None
601
+ renderableApi.operations.append(self._with_transition_fields(operation=operation, transition=transition, isStandard=isStandard).model_copy(update={'operationId': lookupOperationId}))
592
602
  if len(renderableApi.operations) == operationCount:
593
603
  uncoveredTransitions.append(f'{renderableResource.singleName.pascalCaseString} {transition.action}')
594
604
  if uncoveredTransitions:
@@ -237,7 +237,7 @@ class BaseLanguageDefinition:
237
237
  output = [self.kebab_name(name=component.relativeCollectionName)]
238
238
  if component.attribute is None:
239
239
  return output
240
- if component.attribute.isUnique and not component.attribute.isPrimary:
240
+ if (component.attribute.isUnique or component.attribute.isUniqueInParent) and not component.attribute.isPrimary:
241
241
  output.append(self.kebab_name(name=component.attribute.name))
242
242
  if component.attribute.fieldType.rawType not in (PrimitiveTypes.INTEGER, PrimitiveTypes.LONG_INTEGER, PrimitiveTypes.STRING, PrimitiveTypes.UUID):
243
243
  raise KibaException(message=f'ResourceEndpointComponent created with an invalid attribute type of {component.attribute.fieldType}. Valid types are: integer, long integer, string and uuid.')
@@ -279,7 +279,7 @@ class BaseLanguageDefinition:
279
279
  else:
280
280
  singleName, collectionName = target.relativeSingleName, target.relativeCollectionName
281
281
  targetName = singleName if isSingle else collectionName
282
- if isinstance(target, ResourceEndpointComponent) and target.attribute is not None and target.attribute.isUnique and not target.attribute.isPrimary:
282
+ if isinstance(target, ResourceEndpointComponent) and target.attribute is not None and (target.attribute.isUnique or target.attribute.isUniqueInParent) and not target.attribute.isPrimary:
283
283
  targetName += RenderableString(string=['by']) + target.attribute.name
284
284
  if operation.isIndirectAction:
285
285
  targetName = RenderableString(string=['for']) + targetName
@@ -134,6 +134,7 @@ class RenderableField(BaseModel):
134
134
 
135
135
  class RenderableAttribute(RenderableField):
136
136
  isUnique: bool = False
137
+ isUniqueInParent: bool = False
137
138
  isPrimary: bool = False
138
139
  isUpdatable: bool = False
139
140
  isNullable: bool = False
@@ -78,6 +78,7 @@ class Attribute(BaseModel):
78
78
  enumValues: list[str] = []
79
79
  isPrimary: bool = False
80
80
  isUnique: bool = False
81
+ isUniqueInParent: bool = False
81
82
  shouldDisableGetEndpoint: bool = False
82
83
  isUnfilterable: bool = False
83
84
  isUpdatable: bool = False